1 // Copyright (C) 2007-2013 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
24 #pragma warning( disable:4786 )
27 #include "GEOM_Engine.hxx"
29 #include "GEOM_DataMapIteratorOfDataMapOfAsciiStringTransient.hxx"
30 #include "GEOM_Field.hxx"
31 #include "GEOM_Function.hxx"
32 #include "GEOM_ISubShape.hxx"
33 #include "GEOM_PythonDump.hxx"
34 #include "GEOM_Solver.hxx"
35 #include "GEOM_SubShapeDriver.hxx"
36 #include "Sketcher_Profile.hxx"
38 #include <Basics_OCCTVersion.hxx>
40 #include "utilities.h"
42 #include <Basics_Utils.hxx>
44 #include <TDF_Tool.hxx>
45 #include <TDF_Data.hxx>
46 #include <TDF_Reference.hxx>
47 #include <TDF_LabelSequence.hxx>
48 #include <TDataStd_Integer.hxx>
49 #include <TDataStd_ChildNodeIterator.hxx>
50 #include <TFunction_Driver.hxx>
51 #include <TFunction_DriverTable.hxx>
52 #include <TDataStd_ByteArray.hxx>
53 #include <TDataStd_UAttribute.hxx>
54 #include <TDF_ChildIterator.hxx>
55 #include <TDataStd_Comment.hxx>
58 #include <TopTools_IndexedMapOfShape.hxx>
60 #include <TCollection_AsciiString.hxx>
61 #include <TCollection_ExtendedString.hxx>
62 #include <TColStd_SequenceOfAsciiString.hxx>
63 #include <TColStd_MapOfTransient.hxx>
64 #include <TColStd_HSequenceOfInteger.hxx>
66 #include <TColStd_HArray1OfByte.hxx>
67 #include <TColStd_DataMapIteratorOfDataMapOfIntegerTransient.hxx>
69 #include <Resource_DataMapIteratorOfDataMapOfAsciiStringAsciiString.hxx>
73 #include <Standard_Failure.hxx>
74 #include <Standard_ErrorHandler.hxx> // CAREFUL ! position of this file is critic : see Lucien PIGNOLONI / OCC
79 #define O_SQR_BRACKET '['
80 #define C_SQR_BRACKET ']'
81 #define PY_NULL "None"
84 static int MYDEBUG = 0;
86 static int MYDEBUG = 0;
89 typedef std::map< TCollection_AsciiString, TCollection_AsciiString > TSting2StringMap;
90 typedef std::map< TCollection_AsciiString, TObjectData > TSting2ObjDataMap;
91 typedef std::map< TCollection_AsciiString, TObjectData* > TSting2ObjDataPtrMap;
93 static GEOM_Engine* TheEngine = NULL;
96 static TCollection_AsciiString BuildIDFromObject(Handle(GEOM_BaseObject)& theObject)
98 TCollection_AsciiString anID(theObject->GetDocID()), anEntry;
99 TDF_Tool::Entry(theObject->GetEntry(), anEntry);
100 anID+=(TCollection_AsciiString("_")+anEntry);
104 static TCollection_AsciiString BuildID(Standard_Integer theDocID, const char* theEntry)
106 TCollection_AsciiString anID(theDocID);
107 anID+=(TCollection_AsciiString("_")+theEntry);
111 static Standard_Integer ExtractDocID(TCollection_AsciiString& theID)
113 TCollection_AsciiString aDocID = theID.Token("_");
114 if(aDocID.Length() < 1) return -1;
115 return aDocID.IntegerValue();
118 bool ProcessFunction(Handle(GEOM_Function)& theFunction,
119 TCollection_AsciiString& theScript,
120 TCollection_AsciiString& theAfterScript,
121 const TVariablesList& theVariables,
122 const bool theIsPublished,
123 TDF_LabelMap& theProcessed,
124 std::set<TCollection_AsciiString>& theIgnoreObjs,
125 bool& theIsDumpCollected);
127 void ReplaceVariables(TCollection_AsciiString& theCommand,
128 const TVariablesList& theVariables);
130 Handle(TColStd_HSequenceOfInteger) FindEntries(TCollection_AsciiString& theString);
132 void ReplaceEntriesByNames (TCollection_AsciiString& theScript,
133 TSting2ObjDataMap& aEntry2ObjData,
134 const bool theIsPublished,
135 TColStd_SequenceOfAsciiString& theObjListToPublish,
136 Standard_Integer& objectCounter,
137 Resource_DataMapOfAsciiStringAsciiString& aNameToEntry);
139 void AddObjectColors (int theDocID,
140 TCollection_AsciiString& theScript,
141 const TSting2ObjDataMap& theEntry2ObjData);
143 void AddTextures (int theDocID, TCollection_AsciiString& theScript);
145 void PublishObject (TObjectData& theObjectData,
146 TSting2ObjDataMap& theEntry2ObjData,
147 const TSting2ObjDataPtrMap& theStEntry2ObjDataPtr,
148 Resource_DataMapOfAsciiStringAsciiString& theNameToEntry,
149 std::map< int, TCollection_AsciiString >& theEntryToCmdMap,
150 std::set<TCollection_AsciiString>& theMapOfPublished);
152 //================================================================================
154 * \brief Fix up the name of python variable
156 //================================================================================
158 void GEOM_Engine::healPyName( TCollection_AsciiString& pyName,
159 const TCollection_AsciiString& anEntry,
160 Resource_DataMapOfAsciiStringAsciiString& aNameToEntry)
162 const TCollection_AsciiString allowedChars
163 ("qwertyuioplkjhgfdsazxcvbnmQWERTYUIOPLKJHGFDSAZXCVBNM0987654321_");
165 if ( pyName.IsIntegerValue() ) { // pyName must not start with a digit
166 pyName.Insert( 1, 'a' );
168 int p, p2=1; // replace not allowed chars
169 while ((p = pyName.FirstLocationNotInSet(allowedChars, p2, pyName.Length()))) {
170 pyName.SetValue(p, '_');
173 if ( aNameToEntry.IsBound( pyName ) && anEntry != aNameToEntry( pyName ))
174 { // diff objects have same name - make a new name by appending a digit
175 TCollection_AsciiString aName2;
176 Standard_Integer i = 0;
178 aName2 = pyName + "_" + ++i;
179 } while ( aNameToEntry.IsBound( aName2 ) && anEntry != aNameToEntry( aName2 ));
184 //=======================================================================
185 //function : GetTextureGUID
187 //=======================================================================
188 const Standard_GUID& GEOM_Engine::GetTextureGUID()
190 static Standard_GUID anID("FF1BBB01-5D14-4df2-980B-3A668264EA17");
194 //=============================================================================
198 //=============================================================================
199 GEOM_Engine* GEOM_Engine::GetEngine() { return TheEngine; }
201 //=============================================================================
205 //=============================================================================
206 void GEOM_Engine::SetEngine(GEOM_Engine* theEngine) { TheEngine = theEngine; }
208 //=============================================================================
212 //=============================================================================
213 GEOM_Engine::GEOM_Engine()
215 TFunction_DriverTable::Get()->AddDriver(GEOM_Object::GetSubShapeID(), new GEOM_SubShapeDriver());
217 _OCAFApp = new GEOM_Application();
224 GEOM_Engine::~GEOM_Engine()
226 GEOM_DataMapIteratorOfDataMapOfAsciiStringTransient It(_objects);
227 std::list< Handle(GEOM_Object) > objs;
228 for(; It.More(); It.Next())
229 objs.push_back( Handle(GEOM_Object)::DownCast(It.Value()) );
230 std::list< Handle(GEOM_Object) >::iterator objit;
231 for(objit = objs.begin(); objit != objs.end(); ++objit)
232 RemoveObject(*objit);
234 //Close all documents not closed
235 TColStd_DataMapIteratorOfDataMapOfIntegerTransient anItr (_mapIDDocument);
236 for (; anItr.More(); anItr.Next())
239 anItr.Initialize( _mapIDDocument ); // anItr becomes invalid at _mapIDDocument.UnBind(docId)
241 _mapIDDocument.Clear();
245 //=============================================================================
249 //=============================================================================
250 Handle(TDocStd_Document) GEOM_Engine::GetDocument(int theDocID, bool force)
252 Handle(TDocStd_Document) aDoc;
253 if(_mapIDDocument.IsBound(theDocID)) {
254 aDoc = Handle(TDocStd_Document)::DownCast(_mapIDDocument(theDocID));
257 _OCAFApp->NewDocument("SALOME_GEOM", aDoc);
258 aDoc->SetUndoLimit(_UndoLimit);
259 _mapIDDocument.Bind(theDocID, aDoc);
260 TDataStd_Integer::Set(aDoc->Main(), theDocID);
265 //=============================================================================
269 //=============================================================================
270 int GEOM_Engine::GetDocID(Handle(TDocStd_Document) theDocument)
272 if (theDocument.IsNull()) return -1;
273 TColStd_DataMapIteratorOfDataMapOfIntegerTransient anItr (_mapIDDocument);
274 for (; anItr.More(); anItr.Next())
275 if (anItr.Value() == theDocument) return anItr.Key();
280 //=============================================================================
284 //=============================================================================
286 Handle(GEOM_BaseObject) GEOM_Engine::GetObject(int theDocID, const char* theEntry, bool force)
288 Handle(GEOM_BaseObject) anObject;
290 TCollection_AsciiString anID = BuildID(theDocID, theEntry);
292 if (_objects.IsBound(anID)) {
293 anObject = Handle(GEOM_BaseObject)::DownCast(_objects(anID));
296 Handle(TDocStd_Document) aDoc = GetDocument(theDocID, force);
297 if ( !aDoc.IsNull()) {
299 TDF_Tool::Label(aDoc->Main().Data(), theEntry, aLabel, Standard_True);
300 if ( !aLabel.IsNull() ) {
301 int objType = GEOM_BaseObject::GetType( aLabel );
303 case GEOM_FIELD_OBJTYPE: anObject = new GEOM_Field (aLabel); break;
304 case GEOM_FIELD_STEP_OBJTYPE: anObject = new GEOM_FieldStep(aLabel); break;
305 default: anObject = new GEOM_Object (aLabel);
307 _objects.Bind(anID, anObject);
315 //=============================================================================
319 //=============================================================================
321 Handle(GEOM_BaseObject) GEOM_Engine::AddBaseObject(int theDocID, int theType)
323 Handle(TDocStd_Document) aDoc = GetDocument(theDocID);
324 Handle(TDataStd_TreeNode) aRoot = TDataStd_TreeNode::Set(aDoc->Main());
326 // NPAL18604: use existing label to decrease memory usage,
327 // if this label has been freed (object deleted)
328 bool useExisting = false;
330 if (_freeLabels.find(theDocID) != _freeLabels.end()) {
331 std::list<TDF_Label>& aFreeLabels = _freeLabels[theDocID];
332 if (!aFreeLabels.empty()) {
334 aChild = aFreeLabels.front();
335 aFreeLabels.pop_front();
340 aChild = TDF_TagSource::NewChild(aDoc->Main());
343 Handle(GEOM_BaseObject) anObject;
345 case GEOM_FIELD_OBJTYPE: anObject = new GEOM_Field (aChild, theType); break;
346 case GEOM_FIELD_STEP_OBJTYPE: anObject = new GEOM_FieldStep(aChild, theType); break;
347 default: anObject = new GEOM_Object (aChild, theType);
350 //Put an object in the map of created objects
351 TCollection_AsciiString anID = BuildIDFromObject(anObject);
352 if(_objects.IsBound(anID)) _objects.UnBind(anID);
353 _objects.Bind(anID, anObject);
358 //================================================================================
360 * \brief Adds a new object of the type theType in the OCAF document
362 //================================================================================
364 Handle(GEOM_Object) GEOM_Engine::AddObject(int theDocID, int theType)
366 return Handle(GEOM_Object)::DownCast( AddBaseObject(theDocID, theType) );
369 //=============================================================================
373 //=============================================================================
375 Handle(GEOM_Object) GEOM_Engine::AddSubShape(Handle(GEOM_Object) theMainShape,
376 Handle(TColStd_HArray1OfInteger) theIndices,
377 bool isStandaloneOperation)
379 if (theMainShape.IsNull() || theIndices.IsNull()) return NULL;
381 Handle(TDocStd_Document) aDoc = GetDocument(theMainShape->GetDocID());
382 Handle(TDataStd_TreeNode) aRoot = TDataStd_TreeNode::Set(aDoc->Main());
384 // NPAL18604: use existing label to decrease memory usage,
385 // if this label has been freed (object deleted)
386 bool useExisting = false;
388 int aDocID = theMainShape->GetDocID();
389 if (_freeLabels.find(aDocID) != _freeLabels.end()) {
390 std::list<TDF_Label>& aFreeLabels = _freeLabels[aDocID];
391 if (!aFreeLabels.empty()) {
393 aChild = aFreeLabels.front();
394 aFreeLabels.pop_front();
399 aChild = TDF_TagSource::NewChild(aDoc->Main());
402 Handle(GEOM_Function) aMainShape = theMainShape->GetLastFunction();
403 Handle(GEOM_Object) anObject = new GEOM_Object (aChild, 28); //28 is SUBSHAPE type
404 Handle(GEOM_Function) aFunction = anObject->AddFunction(GEOM_Object::GetSubShapeID(), 1);
406 GEOM_ISubShape aSSI (aFunction);
407 aSSI.SetMainShape(aMainShape);
408 aSSI.SetIndices(theIndices);
411 #if OCC_VERSION_LARGE > 0x06010000
414 GEOM_Solver aSolver (GEOM_Engine::GetEngine());
415 if (!aSolver.ComputeFunction(aFunction)) {
416 MESSAGE("GEOM_Engine::AddSubShape Error: Can't build a sub shape");
420 catch (Standard_Failure) {
421 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
422 MESSAGE("GEOM_Engine::AddSubShape Error: " << aFail->GetMessageString());
426 // Put an object in the map of created objects
427 TCollection_AsciiString anID = BuildIDFromObject(anObject);
428 if (_objects.IsBound(anID)) _objects.UnBind(anID);
429 _objects.Bind(anID, anObject);
431 // Put this sub-shape in the list of sub-shapes of theMainShape
432 aMainShape->AddSubShapeReference(aFunction);
434 GEOM::TPythonDump pd (aFunction);
436 if (isStandaloneOperation) {
437 pd << anObject << " = geompy.GetSubShape(" << theMainShape << ", [";
438 Standard_Integer i = theIndices->Lower(), up = theIndices->Upper();
439 for (; i <= up - 1; i++) {
440 pd << theIndices->Value(i) << ", ";
442 pd << theIndices->Value(up) << "])";
450 //=============================================================================
454 //=============================================================================
455 bool GEOM_Engine::RemoveObject(Handle(GEOM_BaseObject)& theObject)
457 if (theObject.IsNull()) return false;
459 int aDocID = theObject->GetDocID();
460 if(!_mapIDDocument.IsBound(aDocID))
461 return false; // document is closed...
463 //Remove an object from the map of available objects
464 TCollection_AsciiString anID = BuildIDFromObject(theObject);
465 if (_objects.IsBound(anID)) _objects.UnBind(anID);
467 // If sub-shape, remove it from the list of sub-shapes of its main shape
468 Handle(GEOM_Object) aGO = Handle(GEOM_Object)::DownCast( theObject );
469 if ( !aGO.IsNull() && !aGO->IsMainShape()) {
470 Handle(GEOM_Function) aFunction = theObject->GetFunction(1);
471 GEOM_ISubShape aSSI (aFunction);
472 Handle(GEOM_Function) aMainShape = aSSI.GetMainShape();
473 //If main shape is not null, then remove
474 if(!aMainShape.IsNull())
475 aMainShape->RemoveSubShapeReference(aFunction);
478 int nb = theObject->GetNbFunctions();
479 Handle(TDataStd_TreeNode) aNode;
480 for (int i = 1; i <= nb; i++) {
481 Handle(GEOM_Function) aFunction = theObject->GetFunction(i);
482 if (aFunction->GetEntry().FindAttribute(GEOM_Function::GetFunctionTreeID(), aNode))
486 TDF_Label aLabel = theObject->GetEntry();
487 aLabel.ForgetAllAttributes(Standard_True);
489 // Remember the label to reuse it then
490 std::list<TDF_Label>& aFreeLabels = _freeLabels[aDocID];
491 if ( aFreeLabels.empty() || aFreeLabels.back() != aLabel )
492 aFreeLabels.push_back(aLabel);
499 //=============================================================================
503 //=============================================================================
504 void GEOM_Engine::Undo(int theDocID)
506 GetDocument(theDocID)->Undo();
509 //=============================================================================
513 //=============================================================================
514 void GEOM_Engine::Redo(int theDocID)
516 GetDocument(theDocID)->Redo();
519 //=============================================================================
523 //=============================================================================
524 bool GEOM_Engine::Save(int theDocID, const char* theFileName)
526 if(!_mapIDDocument.IsBound(theDocID)) return false;
527 Handle(TDocStd_Document) aDoc = Handle(TDocStd_Document)::DownCast(_mapIDDocument(theDocID));
529 _OCAFApp->SaveAs(aDoc, theFileName);
534 //=============================================================================
538 //=============================================================================
539 bool GEOM_Engine::Load(int theDocID, const char* theFileName)
541 Handle(TDocStd_Document) aDoc;
542 if (_OCAFApp->Open(theFileName, aDoc) != PCDM_RS_OK) {
546 aDoc->SetUndoLimit(_UndoLimit);
548 if(_mapIDDocument.IsBound(theDocID)) _mapIDDocument.UnBind(theDocID);
549 _mapIDDocument.Bind(theDocID, aDoc);
551 TDataStd_Integer::Set(aDoc->Main(), theDocID);
556 //=============================================================================
560 //=============================================================================
561 void GEOM_Engine::Close(int theDocID)
563 if (_mapIDDocument.IsBound(theDocID)) {
564 Handle(TDocStd_Document) aDoc = Handle(TDocStd_Document)::DownCast(_mapIDDocument(theDocID));
566 //Remove all GEOM Objects associated to the given document
567 TColStd_SequenceOfAsciiString aSeq;
568 GEOM_DataMapIteratorOfDataMapOfAsciiStringTransient It (_objects);
569 for (; It.More(); It.Next()) {
570 TCollection_AsciiString anObjID (It.Key());
571 Standard_Integer anID = ExtractDocID(anObjID);
572 if (theDocID == anID) aSeq.Append(It.Key());
574 for (Standard_Integer i=1; i<=aSeq.Length(); i++) _objects.UnBind(aSeq.Value(i));
576 // Forget free labels for this document
577 TFreeLabelsList::iterator anIt = _freeLabels.find(theDocID);
578 if (anIt != _freeLabels.end()) {
579 _freeLabels.erase(anIt);
582 _mapIDDocument.UnBind(theDocID);
583 _OCAFApp->Close(aDoc);
588 //=============================================================================
592 //=============================================================================
593 TCollection_AsciiString GEOM_Engine::DumpPython(int theDocID,
594 std::vector<TObjectData>& theObjectData,
595 TVariablesList theVariables,
600 // Set "C" numeric locale to save numbers correctly
601 Kernel_Utils::Localizer loc;
603 TCollection_AsciiString aScript;
604 Handle(TDocStd_Document) aDoc = GetDocument(theDocID);
608 TCollection_AsciiString anEmptyScript;
610 anEmptyScript = "def RebuildData(theStudy): pass\n";
611 return anEmptyScript;
614 aScript = "import GEOM\n";
615 aScript += "from salome.geom import geomBuilder\n";
616 aScript += "import math\n";
617 aScript += "import SALOMEDS\n\n";
619 aScript += "def RebuildData(theStudy):";
621 aScript += "\n\tgeompy = geomBuilder.New(theStudy)\n";
623 AddTextures(theDocID, aScript);
625 Standard_Integer posToInsertGlobalVars = aScript.Length() + 1;
627 // a map containing copies of TObjectData from theObjectData
628 TSting2ObjDataMap aEntry2ObjData;
629 // contains pointers to TObjectData of either aEntry2ObjData or theObjectData; the latter
630 // occures when several StudyEntries correspond to one Entry
631 TSting2ObjDataPtrMap aStEntry2ObjDataPtr;
633 //Resource_DataMapOfAsciiStringAsciiString aEntry2StEntry, aStEntry2Entry, theObjectNames;
634 for (unsigned i = 0; i < theObjectData.size(); ++i )
636 TObjectData& data = theObjectData[i];
637 // look for an object by entry
639 TDF_Tool::Label( aDoc->GetData(), data._entry, L );
640 if ( L.IsNull() ) continue;
641 Handle(GEOM_BaseObject) obj = GEOM_BaseObject::GetObject( L );
643 if ( !obj.IsNull() ) {
644 TSting2ObjDataMap::iterator ent2Data =
645 aEntry2ObjData.insert( std::make_pair( data._entry, data )).first;
647 if ( ent2Data->second._studyEntry == data._studyEntry ) // Entry encounters 1st time
648 aStEntry2ObjDataPtr.insert( std::make_pair( data._studyEntry, & ent2Data->second ));
650 aStEntry2ObjDataPtr.insert( std::make_pair( data._studyEntry, & data ));
654 // collect objects entries to be published
655 TColStd_SequenceOfAsciiString aObjListToPublish;
657 // iterates on functions till critical (that requiers publication of objects)
658 Handle(TDataStd_TreeNode) aNode, aRoot;
659 Handle(GEOM_Function) aFunction;
660 TDF_LabelMap aCheckedFuncMap;
661 std::set< TCollection_AsciiString > anIgnoreObjMap;
663 TCollection_AsciiString aFuncScript;
665 // Mantis issue 0020768
666 Standard_Integer objectCounter = 0;
667 Resource_DataMapOfAsciiStringAsciiString aNameToEntry;
669 if (aDoc->Main().FindAttribute(GEOM_Function::GetFunctionTreeID(), aRoot)) {
670 TDataStd_ChildNodeIterator Itr(aRoot);
671 for (; Itr.More(); Itr.Next()) {
673 aFunction = GEOM_Function::GetFunction(aNode->Label());
674 if (aFunction.IsNull()) {
675 MESSAGE ( "Null function !!!!" );
678 bool isDumpCollected = false;
679 TCollection_AsciiString aCurScript, anAfterScript;
680 if (!ProcessFunction(aFunction, aCurScript, anAfterScript, theVariables,
681 isPublished, aCheckedFuncMap, anIgnoreObjMap,
684 // add function description before dump
685 if (!aCurScript.IsEmpty())
687 if ( aFunction->GetDriverGUID() == GEOM_Object::GetSubShapeID() &&
688 aFuncScript.Length() > aCurScript.Length() )
689 // avoid repeated SubShape...() command at the end
690 if (aFuncScript.Location( aCurScript,
691 aFuncScript.Length() - aCurScript.Length(),
692 aFuncScript.Length()))
693 continue; // aCurScript is already at the end of aFuncScript
694 aFuncScript += aCurScript;
696 if (isDumpCollected ) {
697 // Replace entries by the names
698 ReplaceEntriesByNames( aFuncScript, aEntry2ObjData, isPublished,
699 aObjListToPublish, objectCounter, aNameToEntry );
701 // publish collected objects
702 std::map< int, TCollection_AsciiString > anEntryToCmdMap; // sort publishing commands by study entry
703 int i = 1, n = aObjListToPublish.Length();
704 for ( ; i <= n; i++ )
706 const TCollection_AsciiString& aEntry = aObjListToPublish.Value(i);
707 PublishObject( aEntry2ObjData[aEntry], aEntry2ObjData, aStEntry2ObjDataPtr,
708 aNameToEntry, anEntryToCmdMap, anIgnoreObjMap );
710 // add publishing commands to the script
711 std::map< int, TCollection_AsciiString >::iterator anEntryToCmd = anEntryToCmdMap.begin();
712 for ( ; anEntryToCmd != anEntryToCmdMap.end(); ++anEntryToCmd )
713 aFuncScript += anEntryToCmd->second;
715 // PTv, 0020001 add result objects from RestoreGivenSubShapes into ignore list,
716 // because they will be published during command execution
717 int indx = anAfterScript.Search( "RestoreGivenSubShapes" );
719 indx = anAfterScript.Search( "RestoreSubShapes" );
721 TCollection_AsciiString aSubStr = anAfterScript.SubString(1, indx);
722 Handle(TColStd_HSequenceOfInteger) aSeq = FindEntries(aSubStr);
723 i = 1, n = aSeq->Length();
724 for ( ; i <= n; i+=2) {
725 TCollection_AsciiString anEntry =
726 aSubStr.SubString(aSeq->Value(i), aSeq->Value(i+1));
727 anIgnoreObjMap.insert(anEntry.ToCString());
731 aObjListToPublish.Clear();
732 aScript += aFuncScript;
735 aFuncScript += anAfterScript;
739 // Replace entries by the names
740 aObjListToPublish.Clear();
741 ReplaceEntriesByNames( aFuncScript, aEntry2ObjData, isPublished, aObjListToPublish,
742 objectCounter, aNameToEntry );
744 aScript += aFuncScript;
747 AddObjectColors( theDocID, aScript, aEntry2ObjData );
749 // Make script to publish in study
750 TSting2ObjDataPtrMap::iterator aStEntry2ObjDataPtrIt;
753 std::map< int, TCollection_AsciiString > anEntryToCmdMap; // sort publishing commands by object entry
755 for (aStEntry2ObjDataPtrIt = aStEntry2ObjDataPtr.begin();
756 aStEntry2ObjDataPtrIt != aStEntry2ObjDataPtr.end();
757 ++aStEntry2ObjDataPtrIt)
759 TObjectData* data = aStEntry2ObjDataPtrIt->second;
760 if ( anIgnoreObjMap.count( data->_entry ))
761 continue; // should not be dumped
762 PublishObject( *data, aEntry2ObjData, aStEntry2ObjDataPtr,
763 aNameToEntry, anEntryToCmdMap, anIgnoreObjMap );
765 // add publishing commands to the script
766 std::map< int, TCollection_AsciiString >::iterator anEntryToCmd = anEntryToCmdMap.begin();
767 for ( ; anEntryToCmd != anEntryToCmdMap.end(); ++anEntryToCmd )
768 aScript += anEntryToCmd->second;
771 //RNV: issue 16219: EDF PAL 469: "RemoveFromStudy" Function
772 //Add unpublish command if need
773 TCollection_AsciiString unpublishCmd("\n");
775 unpublishCmd += "\t";
776 unpublishCmd += "geompy.hideInStudy(";
778 for (aStEntry2ObjDataPtrIt = aStEntry2ObjDataPtr.begin();
779 aStEntry2ObjDataPtrIt != aStEntry2ObjDataPtr.end();
780 ++aStEntry2ObjDataPtrIt)
782 TObjectData* data = aStEntry2ObjDataPtrIt->second;
783 if ( data->_unpublished && !data->_pyName.IsEmpty() ) {
784 aScript += unpublishCmd + data->_pyName + ")";
788 //aScript += "\n\tpass\n";
792 // fill _studyEntry2NameMap and build globalVars
793 TCollection_AsciiString globalVars;
794 _studyEntry2NameMap.Clear();
795 for (aStEntry2ObjDataPtrIt = aStEntry2ObjDataPtr.begin();
796 aStEntry2ObjDataPtrIt != aStEntry2ObjDataPtr.end();
797 ++aStEntry2ObjDataPtrIt)
799 const TCollection_AsciiString& studyEntry = aStEntry2ObjDataPtrIt->first;
800 const TObjectData* data = aStEntry2ObjDataPtrIt->second;
801 _studyEntry2NameMap.Bind ( studyEntry, data->_pyName );
802 if ( !globalVars.IsEmpty() )
804 globalVars += data->_pyName;
806 if ( isMultiFile && !globalVars.IsEmpty() ) {
807 globalVars.Insert( 1, "\n\tglobal " );
808 aScript.Insert( posToInsertGlobalVars, globalVars );
814 //=======================================================================
815 //function : GetDumpName
817 //=======================================================================
819 const char* GEOM_Engine::GetDumpName (const char* theStudyEntry) const
821 if ( _studyEntry2NameMap.IsBound( (char*)theStudyEntry ))
822 return _studyEntry2NameMap( (char*)theStudyEntry ).ToCString();
827 //=======================================================================
828 //function : GetAllDumpNames
830 //=======================================================================
832 Handle(TColStd_HSequenceOfAsciiString) GEOM_Engine::GetAllDumpNames() const
834 Handle(TColStd_HSequenceOfAsciiString) aRetSeq = new TColStd_HSequenceOfAsciiString;
836 Resource_DataMapIteratorOfDataMapOfAsciiStringAsciiString it (_studyEntry2NameMap);
837 for (; it.More(); it.Next()) {
838 aRetSeq->Append(it.Value());
844 #define TEXTURE_LABEL_ID 1
845 #define TEXTURE_LABEL_FILE 2
846 #define TEXTURE_LABEL_WIDTH 3
847 #define TEXTURE_LABEL_HEIGHT 4
848 #define TEXTURE_LABEL_DATA 5
850 int GEOM_Engine::addTexture(int theDocID, int theWidth, int theHeight,
851 #if OCC_VERSION_LARGE > 0x06040000 // Porting to OCCT6.5.1
852 const Handle(TColStd_HArray1OfByte)& theTexture,
854 const Handle(TDataStd_HArray1OfByte)& theTexture,
856 const TCollection_AsciiString& theFileName)
858 Handle(TDocStd_Document) aDoc = GetDocument(theDocID);
859 Handle(TDataStd_TreeNode) aRoot = TDataStd_TreeNode::Set(aDoc->Main());
861 // NPAL18604: use existing label to decrease memory usage,
862 // if this label has been freed (object deleted)
863 bool useExisting = false;
865 if (_freeLabels.find(theDocID) != _freeLabels.end()) {
866 std::list<TDF_Label>& aFreeLabels = _freeLabels[theDocID];
867 if (!aFreeLabels.empty()) {
869 aChild = aFreeLabels.front();
870 aFreeLabels.pop_front();
875 aChild = TDF_TagSource::NewChild(aDoc->Main());
878 aChild.ForgetAllAttributes(Standard_True);
879 Handle(TDataStd_TreeNode) node;
880 if ( !aChild.FindAttribute(TDataStd_TreeNode::GetDefaultTreeID(), node ) )
881 node = TDataStd_TreeNode::Set(aChild);
882 TDataStd_UAttribute::Set(aChild, GetTextureGUID());
884 static int aTextureID = 0;
886 TDataStd_Integer::Set(aChild.FindChild(TEXTURE_LABEL_ID), ++aTextureID);
887 TDataStd_Comment::Set(aChild.FindChild(TEXTURE_LABEL_FILE), theFileName);
888 TDataStd_Integer::Set(aChild.FindChild(TEXTURE_LABEL_WIDTH), theWidth);
889 TDataStd_Integer::Set(aChild.FindChild(TEXTURE_LABEL_HEIGHT), theHeight);
891 Handle(TDataStd_ByteArray) anAttr =
892 TDataStd_ByteArray::Set(aChild.FindChild(TEXTURE_LABEL_DATA),
893 theTexture.IsNull() ? 0 : theTexture->Lower(),
894 theTexture.IsNull() ? 0 : theTexture->Upper());
895 anAttr->ChangeArray(theTexture);
900 #if OCC_VERSION_LARGE > 0x06040000 // Porting to OCCT6.5.1
901 Handle(TColStd_HArray1OfByte) GEOM_Engine::getTexture(int theDocID, int theTextureID,
903 Handle(TDataStd_HArray1OfByte) GEOM_Engine::getTexture(int theDocID, int theTextureID,
905 int& theWidth, int& theHeight,
906 TCollection_AsciiString& theFileName)
908 #if OCC_VERSION_LARGE > 0x06040000 // Porting to OCCT6.5.1
909 Handle(TColStd_HArray1OfByte) anArray;
911 Handle(TDataStd_HArray1OfByte) anArray;
913 theWidth = theHeight = 0;
915 Handle(TDocStd_Document) aDoc = GetDocument(theDocID);
917 TDF_ChildIterator anIterator(aDoc->Main(), Standard_True);
919 for (; anIterator.More() && !found; anIterator.Next()) {
920 TDF_Label aTextureLabel = anIterator.Value();
921 if (aTextureLabel.IsAttribute( GetTextureGUID())) {
922 TDF_Label anIDLabel = aTextureLabel.FindChild(TEXTURE_LABEL_ID, Standard_False);
923 Handle(TDataStd_Integer) anIdAttr;
924 if(!anIDLabel.IsNull() && anIDLabel.FindAttribute(TDataStd_Integer::GetID(), anIdAttr) &&
925 anIdAttr->Get() == theTextureID) {
926 TDF_Label aFileLabel = aTextureLabel.FindChild(TEXTURE_LABEL_FILE, Standard_False);
927 TDF_Label aWidthLabel = aTextureLabel.FindChild(TEXTURE_LABEL_WIDTH, Standard_False);
928 TDF_Label aHeightLabel = aTextureLabel.FindChild(TEXTURE_LABEL_HEIGHT, Standard_False);
929 TDF_Label aDataLabel = aTextureLabel.FindChild(TEXTURE_LABEL_DATA, Standard_False);
930 Handle(TDataStd_Integer) aWidthAttr, aHeightAttr;
931 Handle(TDataStd_ByteArray) aTextureAttr;
932 Handle(TDataStd_Comment) aFileAttr;
933 if (!aWidthLabel.IsNull() && aWidthLabel.FindAttribute(TDataStd_Integer::GetID(), aWidthAttr) &&
934 !aHeightLabel.IsNull() && aHeightLabel.FindAttribute(TDataStd_Integer::GetID(), aHeightAttr) &&
935 !aDataLabel.IsNull() && aDataLabel.FindAttribute(TDataStd_ByteArray::GetID(), aTextureAttr)) {
936 theWidth = aWidthAttr->Get();
937 theHeight = aHeightAttr->Get();
938 anArray = aTextureAttr->InternalArray();
940 if (!aFileLabel.IsNull() && aFileLabel.FindAttribute(TDataStd_Comment::GetID(), aFileAttr))
941 theFileName = aFileAttr->Get();
949 std::list<int> GEOM_Engine::getAllTextures(int theDocID)
951 std::list<int> id_list;
953 Handle(TDocStd_Document) aDoc = GetDocument(theDocID);
955 TDF_ChildIterator anIterator(aDoc->Main(), Standard_True);
956 for (; anIterator.More(); anIterator.Next()) {
957 TDF_Label aTextureLabel = anIterator.Value();
958 if (aTextureLabel.IsAttribute( GetTextureGUID())) {
959 TDF_Label anIDLabel = aTextureLabel.FindChild(TEXTURE_LABEL_ID, Standard_False);
960 Handle(TDataStd_Integer) anIdAttr;
961 if(!anIDLabel.IsNull() && anIDLabel.FindAttribute(TDataStd_Integer::GetID(), anIdAttr))
962 id_list.push_back((int)anIdAttr->Get());
968 //===========================================================================
969 // Internal functions
970 //===========================================================================
972 //=============================================================================
974 * MakeCommandfor3DSketcher: Make new command for 3DSketcher
976 //=============================================================================
977 TCollection_AsciiString MakeCommandfor3DSketcher (const TCollection_AsciiString& theDescr )
979 TCollection_AsciiString aNewDescr;
981 TCollection_AsciiString aSubStr = theDescr.Token("\n\t", i);
982 for (; !aSubStr.IsEmpty(); aSubStr = theDescr.Token("\n\t", i)) {
983 if (aSubStr.Search( "Make3DSketcherCommand" ) != -1) {
984 TCollection_AsciiString aResult = aSubStr.Token(" ", 1);
985 // "3DSketcher:CMD[:CMD[:CMD...]]"
986 TCollection_AsciiString aCommand = aSubStr.Token("\"", 2);
988 // Split the command string to separate CMDs
990 TColStd_SequenceOfAsciiString aSequence;
991 if (aCommand.Length()) {
992 TCollection_AsciiString aToken = aCommand.Token(":", icmd);
993 while (aToken.Length() > 0) {
994 aSequence.Append(aToken);
995 aToken = aCommand.Token(":", ++icmd);
999 if (aSequence.Length() > 0) {
1001 aNewDescr += "\n\t";
1003 aNewDescr += "\nsk = geompy.Sketcher3D()";
1004 int nbCMDs = aSequence.Length();
1005 for (icmd = 1; icmd <= nbCMDs; icmd++) {
1006 aNewDescr += "\n\t";
1008 TCollection_AsciiString aCMD = aSequence.Value(icmd);
1010 // Split the CMD into string values
1011 TColStd_SequenceOfAsciiString aStrVals;
1013 TCollection_AsciiString aToken = aCMD.Token(" ", ival);
1014 while (aToken.Length() > 0) {
1015 aStrVals.Append(aToken);
1016 aToken = aCMD.Token(" ", ++ival);
1019 TCollection_AsciiString aCMDpref = aStrVals.Value(1);
1020 if (aCMDpref == "TT") {
1021 aNewDescr += "sk.addPointsAbsolute(";
1022 aNewDescr += aStrVals.Value(2) + ", " + aStrVals.Value(3) + ", " + aStrVals.Value(4) + ")";
1024 else if (aCMDpref == "T") {
1025 aNewDescr += "sk.addPointsRelative(";
1026 aNewDescr += aStrVals.Value(2) + ", " + aStrVals.Value(3) + ", " + aStrVals.Value(4) + ")";
1028 else if (aCMDpref == "WW") {
1029 aNewDescr += "sk.close()";
1031 else if (aCMDpref.Value(1) == 'O'){
1032 TCollection_AsciiString aCMDtrunc = aStrVals.Value(1);
1034 if (aCMDpref.Value(4) == 'C')
1035 aNewDescr += "sk.addPointRadiusAngleH";
1037 aNewDescr += "sk.addPointRadiusAngles";
1038 if (aCMDpref.Value(5) == 'A')
1039 aNewDescr += "Absolute(";
1041 aNewDescr += "Relative(";
1042 aNewDescr += aStrVals.Value(4) + ", " +
1043 aStrVals.Value(2) + ", " + aStrVals.Value(3) + ", " + "\""+aCMDtrunc+"\"" + ")";
1046 aNewDescr += "\n\t";
1047 aNewDescr += aResult + " = sk.wire()";
1049 } // Make3DSketcherCommand
1050 else if (aSubStr.Search( "Make3DSketcher" ) != -1) {
1051 TCollection_AsciiString aResult = aSubStr.Token(" ", 1);
1052 TCollection_AsciiString aCommand = aSubStr.Token("[", 2);
1053 aCommand = aCommand.Token("]", 1);
1055 aNewDescr += "\n\t";
1056 aNewDescr += "\nsk = geompy.Sketcher3D()";
1057 aNewDescr += "\n\t";
1058 aNewDescr += "sk.addPointsAbsolute(";
1059 aNewDescr += aCommand + ")";
1060 aNewDescr += "\n\t";
1061 aNewDescr += aResult + " = sk.wire()";
1065 aNewDescr += "\n\t";
1066 aNewDescr += aSubStr;
1073 //=============================================================================
1075 * ProcessFunction: Dump function description into script
1077 //=============================================================================
1078 bool ProcessFunction(Handle(GEOM_Function)& theFunction,
1079 TCollection_AsciiString& theScript,
1080 TCollection_AsciiString& theAfterScript,
1081 const TVariablesList& theVariables,
1082 const bool theIsPublished,
1083 TDF_LabelMap& theProcessed,
1084 std::set<TCollection_AsciiString>& theIgnoreObjs,
1085 bool& theIsDumpCollected)
1087 theIsDumpCollected = false;
1088 if (theFunction.IsNull()) return false;
1090 if (theProcessed.Contains(theFunction->GetEntry())) return false;
1092 // pass functions, that depends on nonexisting ones
1093 bool doNotProcess = false;
1094 TDF_LabelSequence aSeq;
1095 theFunction->GetDependency(aSeq);
1096 Standard_Integer aLen = aSeq.Length();
1097 for (Standard_Integer i = 1; i <= aLen && !doNotProcess; i++) {
1098 TDF_Label aRefLabel = aSeq.Value(i);
1099 Handle(TDF_Reference) aRef;
1100 if (!aRefLabel.FindAttribute(TDF_Reference::GetID(), aRef)) {
1101 doNotProcess = true;
1104 if (aRef.IsNull() || aRef->Get().IsNull()) {
1105 doNotProcess = true;
1108 Handle(TDataStd_TreeNode) aT;
1109 if (!TDataStd_TreeNode::Find(aRef->Get(), aT)) {
1110 doNotProcess = true;
1113 TDF_Label aDepLabel = aT->Label();
1114 Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(aDepLabel);
1116 if (aFunction.IsNull()) doNotProcess = true;
1117 else if (!theProcessed.Contains(aDepLabel)) doNotProcess = true;
1124 TCollection_AsciiString anObjEntry;
1125 TDF_Tool::Entry(theFunction->GetOwnerEntry(), anObjEntry);
1126 theIgnoreObjs.insert(anObjEntry);
1129 theProcessed.Add(theFunction->GetEntry());
1131 TCollection_AsciiString aDescr = theFunction->GetDescription();
1132 if(aDescr.Length() == 0) return false;
1134 //Check if its internal function which doesn't requires dumping
1135 if(aDescr == "None") return false;
1137 //Check the very specific case of RestoreShape function,
1138 //which is not dumped, but the result can be published by the user.
1139 //We do not publish such objects to decrease danger of dumped script failure.
1140 if(aDescr.Value(1) == '#') {
1141 TCollection_AsciiString anObjEntry;
1142 TDF_Tool::Entry(theFunction->GetOwnerEntry(), anObjEntry);
1143 theIgnoreObjs.insert(anObjEntry);
1147 // 0020001 PTv, check for critical functions, which require dump of objects
1150 // currently, there is only one function "RestoreGivenSubShapes",
1151 // later this check could be replaced by iterations on list of such functions
1152 if (aDescr.Search( "RestoreGivenSubShapes" ) != -1)
1153 theIsDumpCollected = true;
1154 else if (aDescr.Search( "RestoreSubShapes" ) != -1)
1155 theIsDumpCollected = true;
1158 //Replace parameter by notebook variables
1159 ReplaceVariables(aDescr,theVariables);
1161 //Process sketcher functions, replacing string command by calls to Sketcher interface
1162 if ( ( aDescr.Search( "MakeSketcherOnPlane" ) != -1 ) || ( aDescr.Search( "MakeSketcher" ) != -1 ) ) {
1163 Sketcher_Profile aProfile( aDescr.ToCString());
1164 // Make new command for SketcherOnPlane and for Sketcher
1165 aDescr = aProfile.GetDump();
1167 if (aDescr.Search( "Make3DSketcher" ) != -1) {
1168 aDescr = MakeCommandfor3DSketcher ( aDescr );
1171 if ( theIsDumpCollected ) {
1173 bool isBefore = true;
1174 TCollection_AsciiString aSubStr = aDescr.Token("\n\t", i++);
1175 while (!aSubStr.IsEmpty()) {
1177 aSubStr.Search( "RestoreGivenSubShapes" ) == -1 &&
1178 aSubStr.Search( "RestoreSubShapes" ) == -1)
1179 theScript += TCollection_AsciiString("\n\t") + aSubStr;
1181 theAfterScript += TCollection_AsciiString("\n\t") + aSubStr;
1182 aSubStr = aDescr.Token("\n\t", i++);
1186 theScript += "\n\t";
1187 theScript += aDescr;
1192 //=============================================================================
1194 * FindEntries: Returns a sequence of start/end positions of entries in the string
1196 //=============================================================================
1197 Handle(TColStd_HSequenceOfInteger) FindEntries(TCollection_AsciiString& theString)
1199 Handle(TColStd_HSequenceOfInteger) aSeq = new TColStd_HSequenceOfInteger;
1200 Standard_Integer aLen = theString.Length();
1201 Standard_Boolean isFound = Standard_False;
1203 const char* arr = theString.ToCString();
1204 Standard_Integer i = 0, j;
1207 int c = (int)arr[i];
1209 if(c >= 48 && c <= 57) { //Is digit?
1211 isFound = Standard_False;
1212 while((j < aLen) && ((c >= 48 && c <= 57) || c == 58) ) { //Check if it is an entry
1214 if(c == 58) isFound = Standard_True;
1217 if(isFound && arr[j-2] != 58) { // last char should be a diggit
1218 aSeq->Append(i+1); // +1 because AsciiString starts from 1
1229 //=============================================================================
1231 * ReplaceVariables: Replace parameters of the function by variales from
1234 //=============================================================================
1235 void ReplaceVariables(TCollection_AsciiString& theCommand,
1236 const TVariablesList& theVariables)
1239 cout<<"Command : "<<theCommand<<endl;
1242 cout<<"All Entries:"<<endl;
1243 TVariablesList::const_iterator it = theVariables.begin();
1244 for(;it != theVariables.end();it++)
1245 cout<<"\t'"<<(*it).first<<"'"<<endl;
1248 //Additional case - multi-row commands
1249 int aCommandIndex = 1;
1250 while( aCommandIndex < 10 ) { // tmp check
1251 TCollection_AsciiString aCommand = theCommand.Token("\n",aCommandIndex);
1252 if( aCommand.Length() == 0 )
1256 cout<<"Sub-command : "<<aCommand<<endl;
1258 Standard_Integer aStartCommandPos = theCommand.Location(aCommand,1,theCommand.Length());
1259 Standard_Integer aEndCommandPos = aStartCommandPos + aCommand.Length();
1261 //Get Entry of the result object
1262 TCollection_AsciiString anEntry;
1263 if( aCommand.Search("=") != -1 ) // command returns an object
1264 anEntry = aCommand.Token("=",1);
1265 else { // command modifies the object
1266 if (int aStartEntryPos = aCommand.Location(1,'(',1,aCommand.Length()))
1267 if (int aEndEntryPos = aCommand.Location(1,',',aStartEntryPos,aCommand.Length()))
1268 anEntry = aCommand.SubString(aStartEntryPos+1, aEndEntryPos-1);
1270 //Remove white spaces
1271 anEntry.RightAdjust();
1272 anEntry.LeftAdjust();
1274 cout<<"Result entry : '" <<anEntry<<"'"<<endl;
1276 if ( anEntry.IsEmpty() ) {
1281 //Check if result is list of entries - enough to get the first entry in this case
1283 if( anEntry.Value( 1 ) == O_SQR_BRACKET && anEntry.Value( anEntry.Length() ) == C_SQR_BRACKET ) {
1284 while(anEntry.Location(aNbEntries,COMMA,1,anEntry.Length()))
1286 TCollection_AsciiString aSeparator(COMMA);
1287 anEntry = anEntry.Token(aSeparator.ToCString(),1);
1288 anEntry.Remove( 1, 1 );
1289 anEntry.RightAdjust();
1290 anEntry.LeftAdjust();
1292 cout<<"Sub-entry : '" <<anEntry<<"'"<<endl;
1295 //Find variables used for object construction
1296 ObjectStates* aStates = 0;
1297 TVariablesList::const_iterator it = theVariables.find(anEntry);
1298 if( it != theVariables.end() )
1299 aStates = (*it).second;
1303 cout<<"Valiables list empty!!!"<<endl;
1308 TState aVariables = aStates->GetCurrectState();
1311 cout<<"Variables from SObject:"<<endl;
1312 for (int i = 0; i < aVariables.size();i++)
1313 cout<<"\t Variable["<<i<<"] = "<<aVariables[i].myVariable<<endl;
1316 //Calculate total number of parameters
1317 Standard_Integer aTotalNbParams = 1;
1318 while(aCommand.Location(aTotalNbParams,COMMA,1,aCommand.Length()))
1322 cout<<"aTotalNbParams = "<<aTotalNbParams<<endl;
1324 Standard_Integer aFirstParam = aNbEntries;
1326 //Replace parameters by variables
1327 Standard_Integer aStartPos = 0;
1328 Standard_Integer aEndPos = 0;
1330 TCollection_AsciiString aVar, aReplacedVar;
1331 for(Standard_Integer i=aFirstParam;i <= aTotalNbParams;i++) {
1332 //Replace first parameter (bettwen '(' character and first ',' character)
1333 if(i == aFirstParam)
1335 aStartPos = aCommand.Location(O_BRACKET, 1, aCommand.Length()) + 1;
1336 if(aTotalNbParams - aNbEntries > 0 )
1337 aEndPos = aCommand.Location(aFirstParam, COMMA, 1, aCommand.Length());
1339 aEndPos = aCommand.Location(C_BRACKET, 1, aCommand.Length());
1341 //Replace last parameter (bettwen ',' character and ')' character)
1342 else if(i == aTotalNbParams)
1344 aStartPos = aCommand.Location(i-1, COMMA, 1, aCommand.Length()) + 2;
1345 aEndPos = aCommand.Location(C_BRACKET, aStartPos , aCommand.Length());
1347 //Replace other parameters (bettwen two ',' characters)
1348 else if(i != aFirstParam && i != aTotalNbParams )
1350 aStartPos = aCommand.Location(i-1, COMMA, 1, aCommand.Length()) + 2;
1351 aEndPos = aCommand.Location(i, COMMA, 1, aCommand.Length());
1354 if( aCommand.Value( aStartPos ) == O_SQR_BRACKET )
1356 if( aCommand.Value( aEndPos-1 ) == C_SQR_BRACKET )
1358 if ( aStartPos == aEndPos )
1359 continue; // PAL20889: for "[]"
1362 cout<<"aStartPos = "<<aStartPos<<", aEndPos = "<<aEndPos<<endl;
1364 aVar = aCommand.SubString(aStartPos, aEndPos-1);
1369 cout<<"Variable: '"<< aVar <<"'"<<endl;
1371 // specific case for sketcher
1372 if(aVar.Location( TCollection_AsciiString("Sketcher:"), 1, aVar.Length() ) != 0) {
1373 Standard_Integer aNbSections = 1;
1374 while( aVar.Location( aNbSections, ':', 1, aVar.Length() ) )
1378 int aStartSectionPos = 0, aEndSectionPos = 0;
1379 TCollection_AsciiString aSection, aReplacedSection;
1380 for(Standard_Integer aSectionIndex = 1; aSectionIndex <= aNbSections; aSectionIndex++) {
1381 aStartSectionPos = aVar.Location( aSectionIndex, ':', 1, aVar.Length() ) + 1;
1382 if( aSectionIndex != aNbSections )
1383 aEndSectionPos = aVar.Location( aSectionIndex + 1, ':', 1, aVar.Length() );
1385 aEndSectionPos = aVar.Length();
1387 aSection = aVar.SubString(aStartSectionPos, aEndSectionPos-1);
1389 cout<<"aSection: "<<aSection<<endl;
1391 Standard_Integer aNbParams = 1;
1392 while( aSection.Location( aNbParams, ' ', 1, aSection.Length() ) )
1396 int aStartParamPos = 0, aEndParamPos = 0;
1397 TCollection_AsciiString aParameter, aReplacedParameter;
1398 for(Standard_Integer aParamIndex = 1; aParamIndex <= aNbParams; aParamIndex++) {
1399 aStartParamPos = aSection.Location( aParamIndex, ' ', 1, aSection.Length() ) + 1;
1400 if( aParamIndex != aNbParams )
1401 aEndParamPos = aSection.Location( aParamIndex + 1, ' ', 1, aSection.Length() );
1403 aEndParamPos = aSection.Length() + 1;
1406 cout<<"aParamIndex: "<<aParamIndex<<" aStartParamPos: " <<aStartParamPos<<" aEndParamPos: "<<aEndParamPos<<endl;
1408 if ( aStartParamPos == aEndParamPos)
1411 aParameter = aSection.SubString(aStartParamPos, aEndParamPos-1);
1413 cout<<"aParameter: "<<aParameter<<endl;
1415 if(iVar >= aVariables.size())
1418 aReplacedParameter = aVariables[iVar].myVariable;
1419 if(aReplacedParameter.IsEmpty()) {
1424 if(aVariables[iVar].isVariable) {
1425 aReplacedParameter.InsertBefore(1,"'");
1426 aReplacedParameter.InsertAfter(aReplacedParameter.Length(),"'");
1430 cout<<"aSection before : "<<aSection<<endl;
1431 aSection.Remove(aStartParamPos, aEndParamPos - aStartParamPos);
1432 aSection.Insert(aStartParamPos, aReplacedParameter);
1434 cout<<"aSection after : "<<aSection<<endl<<endl;
1438 cout<<"aVar before : "<<aVar<<endl;
1439 aVar.Remove(aStartSectionPos, aEndSectionPos - aStartSectionPos);
1440 aVar.Insert(aStartSectionPos, aSection);
1442 cout<<"aVar after : "<<aVar<<endl<<endl;
1446 cout<<"aCommand before : "<<aCommand<<endl;
1447 aCommand.Remove(aStartPos, aEndPos - aStartPos);
1448 aCommand.Insert(aStartPos, aVar);
1450 cout<<"aCommand after : "<<aCommand<<endl;
1453 } // end of specific case for sketcher
1455 //If parameter is entry or 'None', skip it
1456 if(theVariables.find(aVar) != theVariables.end() || aVar.Search(":") != -1 || aVar == PY_NULL)
1459 if(iVar >= aVariables.size())
1462 aReplacedVar = aVariables[iVar].myVariable;
1463 if(aReplacedVar.IsEmpty()) {
1468 if(aVariables[iVar].isVariable) {
1469 aReplacedVar.InsertBefore(1,"\"");
1470 aReplacedVar.InsertAfter(aReplacedVar.Length(),"\"");
1473 aCommand.Remove(aStartPos, aEndPos - aStartPos);
1474 aCommand.Insert(aStartPos, aReplacedVar);
1478 theCommand.Remove(aStartCommandPos, aEndCommandPos - aStartCommandPos);
1479 theCommand.Insert(aStartCommandPos, aCommand);
1483 aStates->IncrementState();
1487 cout<<"Command : "<<theCommand<<endl;
1490 //=============================================================================
1492 * ReplaceEntriesByNames: Replace object entries by their names
1494 //=============================================================================
1495 void ReplaceEntriesByNames (TCollection_AsciiString& theScript,
1496 TSting2ObjDataMap& aEntry2ObjData,
1497 const bool theIsPublished,
1498 TColStd_SequenceOfAsciiString& theObjListToPublish,
1499 Standard_Integer& objectCounter,
1500 Resource_DataMapOfAsciiStringAsciiString& aNameToEntry)
1502 GEOM_Engine* engine = GEOM_Engine::GetEngine();
1503 Handle(TColStd_HSequenceOfInteger) aSeq = FindEntries(theScript);
1504 Standard_Integer aLen = aSeq->Length(), aStart = 1, aScriptLength = theScript.Length();
1506 //Replace entries by the names
1507 TCollection_AsciiString anUpdatedScript, anEntry, aName, aBaseName("geomObj_"),
1508 allowedChars ("qwertyuioplkjhgfdsazxcvbnmQWERTYUIOPLKJHGFDSAZXCVBNM0987654321_");
1509 if (aLen == 0) anUpdatedScript = theScript;
1511 for (Standard_Integer i = 1; i <= aLen; i+=2) {
1512 anUpdatedScript += theScript.SubString(aStart, aSeq->Value(i)-1);
1513 anEntry = theScript.SubString(aSeq->Value(i), aSeq->Value(i+1));
1514 theObjListToPublish.Append( anEntry );
1516 TObjectData& data = aEntry2ObjData[ anEntry ];
1517 if ( data._pyName.IsEmpty() ) { // encounted for the 1st time
1518 if ( !data._name.IsEmpty() ) { // published object
1519 data._pyName = data._name;
1520 engine->healPyName( data._pyName, anEntry, aNameToEntry);
1524 data._pyName = aBaseName + TCollection_AsciiString(++objectCounter);
1525 } while(aNameToEntry.IsBound(data._pyName));
1529 aNameToEntry.Bind(data._pyName, anEntry); // to detect same name of diff objects
1531 anUpdatedScript += data._pyName;
1532 aStart = aSeq->Value(i+1) + 1;
1535 //Add final part of the script
1536 if (aLen && aSeq->Value(aLen) < aScriptLength)
1537 anUpdatedScript += theScript.SubString(aSeq->Value(aLen)+1, aScriptLength); // mkr : IPAL11865
1539 theScript = anUpdatedScript;
1542 //=============================================================================
1544 * AddObjectColors: Add color to objects
1546 //=============================================================================
1547 void AddObjectColors (int theDocID,
1548 TCollection_AsciiString& theScript,
1549 const TSting2ObjDataMap& theEntry2ObjData)
1551 GEOM_Engine* engine = GEOM_Engine::GetEngine();
1552 Handle(TDocStd_Document) aDoc = engine->GetDocument(theDocID);
1554 TSting2ObjDataMap::const_iterator anEntryToNameIt;
1555 for (anEntryToNameIt = theEntry2ObjData.begin();
1556 anEntryToNameIt!= theEntry2ObjData.end();
1559 const TCollection_AsciiString& aEntry = anEntryToNameIt->first;
1560 const TCollection_AsciiString& aName = anEntryToNameIt->second._pyName;
1563 TDF_Tool::Label( aDoc->GetData(), aEntry, L );
1567 Handle(GEOM_Object) obj = GEOM_Object::GetObject( L );
1571 bool anAutoColor = obj->GetAutoColor();
1574 TCollection_AsciiString aCommand( "\n\t" );
1575 aCommand += aName + ".SetAutoColor(1)";
1576 theScript += aCommand.ToCString();
1579 GEOM_Object::Color aColor = obj->GetColor();
1580 if ( aColor.R >= 0 && aColor.G >= 0 && aColor.B >= 0 )
1582 TCollection_AsciiString aCommand( "\n\t" );
1583 aCommand += aName + ".SetColor(SALOMEDS.Color(" + aColor.R + "," + aColor.G + "," + aColor.B + "))";
1584 theScript += aCommand.ToCString();
1587 Aspect_TypeOfMarker aMarkerType = obj->GetMarkerType();
1588 if (aMarkerType >= Aspect_TOM_POINT && aMarkerType < Aspect_TOM_USERDEFINED) {
1589 TCollection_AsciiString aCommand( "\n\t" );
1590 aCommand += aName + ".SetMarkerStd(";
1591 switch (aMarkerType) {
1592 case Aspect_TOM_POINT: aCommand += "GEOM.MT_POINT"; break;
1593 case Aspect_TOM_PLUS: aCommand += "GEOM.MT_PLUS"; break;
1594 case Aspect_TOM_STAR: aCommand += "GEOM.MT_STAR"; break;
1595 case Aspect_TOM_O: aCommand += "GEOM.MT_O"; break;
1596 case Aspect_TOM_X: aCommand += "GEOM.MT_X"; break;
1597 case Aspect_TOM_O_POINT: aCommand += "GEOM.MT_O_POINT"; break;
1598 case Aspect_TOM_O_PLUS: aCommand += "GEOM.MT_O_PLUS"; break;
1599 case Aspect_TOM_O_STAR: aCommand += "GEOM.MT_O_STAR"; break;
1600 case Aspect_TOM_O_X: aCommand += "GEOM.MT_O_X"; break;
1601 case Aspect_TOM_BALL: aCommand += "GEOM.MT_BALL"; break;
1602 case Aspect_TOM_RING1: aCommand += "GEOM.MT_RING1"; break;
1603 case Aspect_TOM_RING2: aCommand += "GEOM.MT_RING2"; break;
1604 case Aspect_TOM_RING3: aCommand += "GEOM.MT_RING3"; break;
1605 default: aCommand += "GEOM.MT_NONE"; break; // just for completeness, should not get here
1608 int aSize = (int)( obj->GetMarkerSize()/0.5 ) - 1;
1610 case 1: aCommand += "GEOM.MS_10"; break;
1611 case 2: aCommand += "GEOM.MS_15"; break;
1612 case 3: aCommand += "GEOM.MS_20"; break;
1613 case 4: aCommand += "GEOM.MS_25"; break;
1614 case 5: aCommand += "GEOM.MS_30"; break;
1615 case 6: aCommand += "GEOM.MS_35"; break;
1616 case 7: aCommand += "GEOM.MS_40"; break;
1617 case 8: aCommand += "GEOM.MS_45"; break;
1618 case 9: aCommand += "GEOM.MS_50"; break;
1619 case 10: aCommand += "GEOM.MS_55"; break;
1620 case 11: aCommand += "GEOM.MS_60"; break;
1621 case 12: aCommand += "GEOM.MS_65"; break;
1622 case 13: aCommand += "GEOM.MS_70"; break;
1623 default: aCommand += "GEOM.MS_NONE"; break;
1626 theScript += aCommand.ToCString();
1628 else if (aMarkerType == Aspect_TOM_USERDEFINED) {
1629 int aMarkerTextureID = obj->GetMarkerTexture();
1630 if (aMarkerTextureID >= 0) {
1631 TCollection_AsciiString aCommand( "\n\t" );
1632 aCommand += aName + ".SetMarkerTexture(texture_map[";
1633 aCommand += aMarkerTextureID;
1635 theScript += aCommand.ToCString();
1641 #if OCC_VERSION_LARGE > 0x06040000 // Porting to OCCT6.5.1
1642 static TCollection_AsciiString pack_data (const Handle(TColStd_HArray1OfByte)& aData)
1644 static TCollection_AsciiString pack_data (const Handle(TDataStd_HArray1OfByte)& aData)
1647 TCollection_AsciiString stream;
1648 if (!aData.IsNull()) {
1649 for (Standard_Integer i = aData->Lower(); i <= aData->Upper(); i++) {
1650 Standard_Byte byte = aData->Value(i);
1651 TCollection_AsciiString strByte = "";
1652 for (int j = 0; j < 8; j++)
1653 strByte.Prepend((byte & (1<<j)) ? "1" : "0");
1660 void AddTextures (int theDocID, TCollection_AsciiString& theScript)
1662 GEOM_Engine* engine = GEOM_Engine::GetEngine();
1663 std::list<int> allTextures = engine->getAllTextures(theDocID);
1664 std::list<int>::const_iterator it;
1666 if (allTextures.size() > 0) {
1667 theScript += "\n\ttexture_map = {}\n";
1669 for (it = allTextures.begin(); it != allTextures.end(); ++it) {
1670 if (*it <= 0) continue;
1671 Standard_Integer aWidth, aHeight;
1672 TCollection_AsciiString aFileName;
1673 #if OCC_VERSION_LARGE > 0x06040000 // Porting to OCCT6.5.1
1674 Handle(TColStd_HArray1OfByte) aTexture =
1676 Handle(TDataStd_HArray1OfByte) aTexture =
1678 engine->getTexture(theDocID, *it, aWidth, aHeight, aFileName);
1679 if (aWidth > 0 && aHeight > 0 && !aTexture.IsNull() && aTexture->Length() > 0 ) {
1680 TCollection_AsciiString aCommand = "\n\t";
1681 aCommand += "texture_map["; aCommand += *it; aCommand += "] = ";
1682 if (aFileName != "" ) {
1683 aCommand += "geompy.LoadTexture(\"";
1684 aCommand += aFileName.ToCString();
1688 aCommand += "geompy.AddTexture(";
1689 aCommand += aWidth; aCommand += ", "; aCommand += aHeight; aCommand += ", \"";
1690 aCommand += pack_data(aTexture);
1693 theScript += aCommand;
1700 //=============================================================================
1702 * PublishObject: publish object in study script
1704 //=============================================================================
1705 void PublishObject (TObjectData& theObjectData,
1706 TSting2ObjDataMap& theEntry2ObjData,
1707 const TSting2ObjDataPtrMap& theStEntry2ObjDataPtr,
1708 Resource_DataMapOfAsciiStringAsciiString& theNameToEntry,
1709 std::map< int, TCollection_AsciiString >& theEntryToCmdMap,
1710 std::set< TCollection_AsciiString>& theIgnoreMap)
1712 GEOM_Engine* engine = GEOM_Engine::GetEngine();
1713 if ( theObjectData._studyEntry.IsEmpty() )
1714 return; // was not published
1715 if ( theIgnoreMap.count( theObjectData._entry ) )
1716 return; // not to publish
1718 TCollection_AsciiString aCommand("\n\tgeompy.");
1720 // find a father entry
1721 TObjectData* aFatherData = 0;
1722 TCollection_AsciiString aFatherStudyEntry =
1723 theObjectData._studyEntry.SubString( 1, theObjectData._studyEntry.SearchFromEnd(":") - 1 );
1724 TSting2ObjDataPtrMap::const_iterator stEntry2DataPtr =
1725 theStEntry2ObjDataPtr.find( aFatherStudyEntry );
1726 if ( stEntry2DataPtr != theStEntry2ObjDataPtr.end() )
1727 aFatherData = stEntry2DataPtr->second;
1729 const int geomObjDepth = 3;
1731 // treat multiply published object
1732 if ( theObjectData._pyName.IsEmpty() )
1734 TObjectData& data0 = theEntry2ObjData[ theObjectData._entry ];
1735 if ( data0._pyName.IsEmpty() ) return; // something wrong
1737 theObjectData._pyName = theObjectData._name;
1738 engine->healPyName( theObjectData._pyName, theObjectData._entry, theNameToEntry);
1740 TCollection_AsciiString aCreationCommand("\n\t");
1741 aCreationCommand += theObjectData._pyName + " = " + data0._pyName;
1743 // store aCreationCommand before publishing commands
1744 int tag = theObjectData._entry.Token( ":", geomObjDepth ).IntegerValue();
1745 theEntryToCmdMap.insert( std::make_pair( tag + 2*theEntry2ObjData.size(), aCreationCommand ));
1749 if ( aFatherData && !aFatherData->_pyName.IsEmpty() ) {
1750 aCommand += "addToStudyInFather( ";
1751 aCommand += aFatherData->_pyName + ", ";
1754 aCommand += "addToStudy( ";
1756 aCommand += theObjectData._pyName + ", '" + theObjectData._name + "' )";
1758 // bind a command to the study entry
1759 int tag = theObjectData._entry.Token( ":", geomObjDepth ).IntegerValue();
1760 theEntryToCmdMap.insert( std::make_pair( tag, aCommand ));
1762 theObjectData._studyEntry.Clear(); // not to publish any more
1765 //================================================================================
1767 * \brief Constructor
1769 //================================================================================
1770 ObjectStates::ObjectStates()
1775 //================================================================================
1779 //================================================================================
1780 ObjectStates::~ObjectStates()
1784 //================================================================================
1786 * \brief Return current object state
1787 * \retval state - Object state (vector of notebook variable)
1789 //================================================================================
1790 TState ObjectStates::GetCurrectState() const
1792 if(_states.size() > _dumpstate)
1793 return _states[_dumpstate];
1797 //================================================================================
1799 * \brief Add new object state
1800 * \param theState - Object state (vector of notebook variable)
1802 //================================================================================
1803 void ObjectStates::AddState(const TState &theState)
1805 _states.push_back(theState);
1808 //================================================================================
1810 * \brief Increment object state
1812 //================================================================================
1813 void ObjectStates::IncrementState()