1 #include "GEOM_Engine.hxx"
2 #include <TCollection_ExtendedString.hxx>
3 #include <Interface_DataMapIteratorOfDataMapOfIntegerTransient.hxx>
5 #include <TDF_Tool.hxx>
6 #include <TDF_Data.hxx>
7 #include <TDataStd_Integer.hxx>
8 #include <TDataStd_ChildNodeIterator.hxx>
11 #include <TFunction_Driver.hxx>
12 #include <TFunction_DriverTable.hxx>
14 #include <TCollection_AsciiString.hxx>
15 #include <TColStd_SequenceOfAsciiString.hxx>
16 #include <TopTools_IndexedMapOfShape.hxx>
19 #include "GEOM_SubShapeDriver.hxx"
20 #include "GEOM_ISubShape.hxx"
21 #include "GEOM_Function.hxx"
22 #include "GEOM_DataMapIteratorOfDataMapOfAsciiStringTransient.hxx"
25 #include "utilities.h"
27 static GEOM_Engine* TheEngine = NULL;
29 static TCollection_AsciiString BuildIDFromObject(Handle(GEOM_Object)& theObject)
31 TCollection_AsciiString anID(theObject->GetDocID()), anEntry;
32 TDF_Tool::Entry(theObject->GetEntry(), anEntry);
33 anID+=(TCollection_AsciiString("_")+anEntry);
38 static TCollection_AsciiString BuildID(Standard_Integer theDocID, char* theEntry)
40 TCollection_AsciiString anID(theDocID);
41 anID+=(TCollection_AsciiString("_")+theEntry);
45 static Standard_Integer ExtractDocID(TCollection_AsciiString& theID)
47 TCollection_AsciiString aDocID = theID.Token("_");
48 if(aDocID.Length() < 1) return -1;
49 return aDocID.IntegerValue();
52 //=============================================================================
56 //=============================================================================
57 GEOM_Engine* GEOM_Engine::GetEngine() { return TheEngine; }
60 //=============================================================================
64 //=============================================================================
65 void GEOM_Engine::SetEngine(GEOM_Engine* theEngine) { TheEngine = theEngine; }
67 //=============================================================================
71 //=============================================================================
72 GEOM_Engine::GEOM_Engine()
74 TFunction_DriverTable::Get()->AddDriver(GEOM_Object::GetSubShapeID(), new GEOM_SubShapeDriver());
76 _OCAFApp = new GEOM_Application();
80 //=============================================================================
84 //=============================================================================
85 Handle(TDocStd_Document) GEOM_Engine::GetDocument(int theDocID)
87 Handle(TDocStd_Document) aDoc;
88 if(!_mapIDDocument.IsBound(theDocID)) {
89 _OCAFApp->NewDocument("SALOME_GEOM", aDoc);
90 aDoc->SetUndoLimit(_UndoLimit);
91 _mapIDDocument.Bind(theDocID, aDoc);
92 TDataStd_Integer::Set(aDoc->Main(), theDocID);
95 return Handle(TDocStd_Document)::DownCast(_mapIDDocument(theDocID));
98 //=============================================================================
102 //=============================================================================
103 int GEOM_Engine::GetDocID(Handle(TDocStd_Document) theDocument)
105 if(theDocument.IsNull()) return -1;
106 for(Interface_DataMapIteratorOfDataMapOfIntegerTransient anItr(_mapIDDocument); anItr.More(); anItr.Next())
107 if(anItr.Value() == theDocument) return anItr.Key();
113 //=============================================================================
117 //=============================================================================
118 Handle(GEOM_Object) GEOM_Engine::GetObject(int theDocID, char* theEntry)
120 TCollection_AsciiString anID = BuildID(theDocID, theEntry);
121 if(_objects.IsBound(anID)) return Handle(GEOM_Object)::DownCast(_objects(anID));
124 Handle(TDocStd_Document) aDoc = GetDocument(theDocID);
125 TDF_Tool::Label(aDoc->Main().Data(), theEntry, aLabel, Standard_True);
126 Handle(GEOM_Object) anObject = new GEOM_Object(aLabel);
128 _objects.Bind(anID, anObject);
133 //=============================================================================
137 //=============================================================================
138 Handle(GEOM_Object) GEOM_Engine::AddObject(int theDocID, int theType)
140 Handle(TDocStd_Document) aDoc = GetDocument(theDocID);
141 Handle(TDataStd_TreeNode) aRoot = TDataStd_TreeNode::Set(aDoc->Main());
143 TDF_Label aChild = TDF_TagSource::NewChild(aDoc->Main());
144 Handle(GEOM_Object) anObject = new GEOM_Object(aChild, theType);
146 //Put an object in the map of created objects
147 TCollection_AsciiString anID = BuildIDFromObject(anObject);
148 if(_objects.IsBound(anID)) _objects.UnBind(anID);
149 _objects.Bind(anID, anObject);
154 //=============================================================================
158 //=============================================================================
159 Handle(GEOM_Object) GEOM_Engine::AddSubShape(Handle(GEOM_Object) theMainShape, Handle(TColStd_HArray1OfInteger) theIndices)
161 if(theMainShape.IsNull() || theIndices.IsNull()) return NULL;
163 Handle(TDocStd_Document) aDoc = GetDocument(theMainShape->GetDocID());
164 Handle(TDataStd_TreeNode) aRoot = TDataStd_TreeNode::Set(aDoc->Main());
166 TDF_Label aChild = TDF_TagSource::NewChild(aDoc->Main());
168 Handle(GEOM_Function) aMainShape = theMainShape->GetLastFunction();
169 Handle(GEOM_Object) anObject = new GEOM_Object(aChild, 28); //28 is SUBSHAPE type
170 Handle(GEOM_Function) aFunction = anObject->AddFunction(GEOM_Object::GetSubShapeID(), 1);
172 GEOM_ISubShape aSSI(aFunction);
173 aSSI.SetMainShape(aMainShape);
174 aSSI.SetIndices(theIndices);
176 //Put an object in the map of created objects
177 TCollection_AsciiString anID = BuildIDFromObject(anObject);
178 if(_objects.IsBound(anID)) _objects.UnBind(anID);
179 _objects.Bind(anID, anObject);
181 TCollection_AsciiString anEntry, aDescr("");
182 TDF_Tool::Entry(anObject->GetEntry(), anEntry);
184 aDescr += " = geom.AddSubShape(";
185 TDF_Tool::Entry(theMainShape->GetEntry(), anEntry);
186 aDescr += (anEntry+", ");
188 for(Standard_Integer i=theIndices->Lower(); i<=theIndices->Upper(); i++) {
189 aDescr += (TCollection_AsciiString(theIndices->Value(i))+", ");
191 aDescr.Trunc(aDescr.Length()-1);
193 aFunction->SetDescription(aDescr);
198 //=============================================================================
202 //=============================================================================
203 bool GEOM_Engine::RemoveObject(Handle(GEOM_Object) theObject)
205 if(!theObject) return false;
207 //Remove an object from the map of available objects
208 TCollection_AsciiString anID = BuildIDFromObject(theObject);
209 if(_objects.IsBound(anID)) _objects.UnBind(anID);
211 TDF_Label aLabel = theObject->GetEntry();
212 aLabel.ForgetAllAttributes(Standard_True);
220 //=============================================================================
224 //=============================================================================
225 void GEOM_Engine::Undo(int theDocID)
227 GetDocument(theDocID)->Undo();
231 //=============================================================================
235 //=============================================================================
236 void GEOM_Engine::Redo(int theDocID)
238 GetDocument(theDocID)->Redo();
241 //=============================================================================
245 //=============================================================================
246 bool GEOM_Engine::Save(int theDocID, char* theFileName)
248 if(!_mapIDDocument.IsBound(theDocID)) return false;
249 Handle(TDocStd_Document) aDoc = Handle(TDocStd_Document)::DownCast(_mapIDDocument(theDocID));
251 _OCAFApp->SaveAs(aDoc, theFileName);
256 //=============================================================================
260 //=============================================================================
261 bool GEOM_Engine::Load(int theDocID, char* theFileName)
263 Handle(TDocStd_Document) aDoc;
264 if(_OCAFApp->Open(theFileName, aDoc) != CDF_RS_OK) return false;
266 aDoc->SetUndoLimit(_UndoLimit);
268 if(_mapIDDocument.IsBound(theDocID)) _mapIDDocument.UnBind(theDocID);
269 _mapIDDocument.Bind(theDocID, aDoc);
271 TDataStd_Integer::Set(aDoc->Main(), theDocID);
276 //=============================================================================
280 //=============================================================================
281 void GEOM_Engine::Close(int theDocID)
283 if(_mapIDDocument.IsBound(theDocID)) {
284 Handle(TDocStd_Document) aDoc = Handle(TDocStd_Document)::DownCast(_mapIDDocument(theDocID));
286 //Remove all GEOM Objects associated to the given document
287 TColStd_SequenceOfAsciiString aSeq;
288 GEOM_DataMapIteratorOfDataMapOfAsciiStringTransient It(_objects);
289 for(; It.More(); It.Next()) {
290 TCollection_AsciiString anObjID(It.Key());
291 Standard_Integer anID = ExtractDocID(anObjID);
292 if(theDocID == anID) aSeq.Append(It.Key());
294 for(Standard_Integer i=1; i<=aSeq.Length(); i++) _objects.UnBind(aSeq.Value(i));
296 _mapIDDocument.UnBind(theDocID);
297 _OCAFApp->Close(aDoc);