1 // Created on: 2008-06-07
2 // Created by: Pavel TELKOV
3 // Copyright (c) 2008-2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
16 #include <TObjDRAW.ixx>
19 #include <TObj_Model.hxx>
20 #include <TObj_TModel.hxx>
21 #include <TObj_Object.hxx>
22 #include <TObj_ObjectIterator.hxx>
23 #include <TObj_TNameContainer.hxx>
25 #include <Message_MsgFile.hxx>
26 #include <Standard_Type.hxx>
27 #include <TColStd_HArray1OfReal.hxx>
28 #include <TCollection_ExtendedString.hxx>
32 #include <DDocStd.hxx>
33 #include <DDocStd_DrawDocument.hxx>
35 #include <TDF_Tool.hxx>
36 #include <TDF_Data.hxx>
37 #include <TDataStd_Name.hxx>
38 #include <TDocStd_Document.hxx>
40 #include <Draw_PluginMacro.hxx>
42 // avoid warnings on 'extern "C"' functions returning C++ classes
44 #pragma warning(4:4190)
47 //=======================================================================
48 // Section: General commands
49 //=======================================================================
51 class Handle(TObjDRAW_Model);
53 //! simple model with redefined pure virtual method
54 class TObjDRAW_Model : public TObj_Model
57 Standard_EXPORT TObjDRAW_Model()
60 virtual Standard_EXPORT Handle(TObj_Model) NewEmpty()
62 return new TObjDRAW_Model();
67 DEFINE_STANDARD_RTTI(TObjDRAW_Model, TObj_Model)
70 DEFINE_STANDARD_HANDLE (TObjDRAW_Model,TObj_Model)
73 class Handle(TObjDRAW_Object);
75 //! simple object to check API and features of TObj_Object
76 class TObjDRAW_Object : public TObj_Object
79 //! enumeration for the ranks of label under Data section.
82 DataTag_First = TObj_Object::DataTag_Last,
85 DataTag_Last = DataTag_First + 100
88 // enumeration for the ranks of label under Reference section.
91 RefTag_First = TObj_Object::RefTag_Last,
92 RefTag_Other, //!< here we test only one refrence to other
93 RefTag_Last = RefTag_First + 100
96 //! enumeration for the ranks of label under Children section.
99 ChildTag_First = TObj_Object::ChildTag_Last,
100 ChildTag_Child, //!< here we test only one child (or one branch of children)
101 ChildTag_Last = ChildTag_First + 100
105 Standard_EXPORT TObjDRAW_Object(const TDF_Label& theLab)
106 : TObj_Object( theLab ) {}
109 Standard_EXPORT void SetInt( const Standard_Integer theVal )
110 { setInteger( theVal, DataTag_IntVal ); }
111 //! returns int value
112 Standard_EXPORT Standard_Integer GetInt() const
113 { return getInteger( DataTag_IntVal ); }
115 //! sets array of real
116 Standard_EXPORT void SetRealArr( const Handle(TColStd_HArray1OfReal)& theHArr )
117 { setArray( theHArr, DataTag_RealArr ); }
118 //! returns array of real
119 Standard_EXPORT Handle(TColStd_HArray1OfReal) GetRealArr() const
120 { return getRealArray( 0, DataTag_RealArr ); }
122 //! set reference to other object
123 Standard_EXPORT void SetRef( const Handle(TObj_Object)& theOther )
124 { setReference( theOther, RefTag_Other ); }
126 Standard_EXPORT Handle(TObj_Object) GetRef() const
127 { return getReference( RefTag_Other ); }
130 Standard_EXPORT Handle(TObj_Object) AddChild()
132 TDF_Label aChL = getChildLabel( ChildTag_Child ).NewChild();
133 return new TObjDRAW_Object( aChL );
137 // Persistence of TObj object
138 DECLARE_TOBJOCAF_PERSISTENCE(TObjDRAW_Object,TObj_Object)
141 // Declaration of CASCADE RTTI
142 DEFINE_STANDARD_RTTI (TObjDRAW_Object, TObj_Object)
146 // Definition of HANDLE object using Standard_DefineHandle.hxx
147 DEFINE_STANDARD_HANDLE (TObjDRAW_Object,TObj_Object)
150 IMPLEMENT_TOBJOCAF_PERSISTENCE(TObjDRAW_Object)
152 //=======================================================================
153 //function : newModel
155 //=======================================================================
156 static Standard_Integer newModel (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
158 if (argc < 2) {di<<"Use "<< argv[0] << "nameDoc\n";return 1;}
160 Handle(TDocStd_Document) D;
161 Handle(DDocStd_DrawDocument) DD;
163 if (!DDocStd::GetDocument(argv[1],D,Standard_False)) {
164 Handle(TObjDRAW_Model) aModel = new TObjDRAW_Model();
166 D = aModel->GetDocument();
167 DD = new DDocStd_DrawDocument(D);
168 TDataStd_Name::Set(D->GetData()->Root(),argv[1]);
169 Draw::Set(argv[1],DD);
170 di << "document " << argv[1] << " created" << "\n";
172 else di << argv[1] << " is already a document" << "\n";
177 static Handle(TObj_Model) getModelByName( const char* theName )
179 Handle(TObj_Model) aModel;
180 Handle(TDocStd_Document) D;
181 if (!DDocStd::GetDocument(theName,D)) return aModel;
183 TDF_Label aLabel = D->Main();
184 Handle(TObj_TModel) aModelAttr;
185 if (!aLabel.IsNull() && aLabel.FindAttribute(TObj_TModel::GetID(), aModelAttr))
186 aModel = aModelAttr->Model();
190 //=======================================================================
191 //function : saveModel
193 //=======================================================================
194 static Standard_Integer saveModel (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
196 if (argc < 2) {di<<"Use "<< argv[0] << "nameDoc [fileName]\n";return 1;}
198 Handle(TObj_Model) aModel = getModelByName(argv[1]);
199 if ( aModel.IsNull() ) return 1;
200 Standard_Boolean isSaved = Standard_False;
202 isSaved = aModel->SaveAs( argv[2] );
204 isSaved = aModel->Save();
207 di << "Error: Document not saved" << "\n";
213 //=======================================================================
214 //function : loadModel
216 //=======================================================================
217 static Standard_Integer loadModel (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
219 if (argc < 3) {di<<"Use "<< argv[0] << "nameDoc fileName\n";return 1;}
221 Standard_Boolean isLoaded = Standard_False;
222 Handle(TObj_Model) aModel = getModelByName(argv[1]);
223 if ( aModel.IsNull() )
226 aModel = new TObjDRAW_Model();
227 isLoaded = aModel->Load( argv[2] );
230 Handle(TDocStd_Document) D = aModel->GetDocument();
231 Handle(DDocStd_DrawDocument) DD = new DDocStd_DrawDocument(D);
233 TDataStd_Name::Set(D->GetData()->Root(),argv[1]);
234 Draw::Set(argv[1],DD);
238 isLoaded = aModel->Load( argv[2] );
242 di << "Error: Document not loaded" << "\n";
249 //=======================================================================
250 //function : closeModel
252 //=======================================================================
253 static Standard_Integer closeModel (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
255 if (argc < 2) {di<<"Use "<< argv[0] << "nameDoc\n";return 1;}
257 Handle(TObj_Model) aModel = getModelByName(argv[1]);
258 if ( aModel.IsNull() ) return 1;
264 //=======================================================================
267 //=======================================================================
268 static Standard_Integer addObj (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
270 if (argc < 3) {di<<"Use "<< argv[0] << "DocName ObjName\n";return 1;}
271 Handle(TObj_Model) aModel = getModelByName(argv[1]);
272 if ( aModel.IsNull() ) return 1;
273 Handle(TObjDRAW_Object) tObj =
274 new TObjDRAW_Object( aModel->GetMainPartition()->NewLabel() );
277 di << "Error: Object not created" << "\n";
280 tObj->SetName( argv[2] );
285 static Handle(TObjDRAW_Object) getObjByName( const char* modelName, const char* objName )
287 Handle(TObjDRAW_Object) tObj;
288 Handle(TObj_Model) aModel = getModelByName(modelName);
289 if ( aModel.IsNull() )
291 Handle(TCollection_HExtendedString) aName = new TCollection_HExtendedString( objName );
292 Handle(TObj_TNameContainer) aDict;
293 tObj = Handle(TObjDRAW_Object)::DownCast( aModel->FindObject(aName, aDict) );
297 //=======================================================================
300 //=======================================================================
301 static Standard_Integer setVal (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
303 if (argc < 4) {di<<"Use "<< argv[0] << "DocName ObjName1 intVal | -r N r1 r2 ... rN\n";return 1;}
304 Handle(TObjDRAW_Object) tObj = getObjByName( argv[1], argv[2] );
307 di << "Error: Object " << argv[2] << " not found" << "\n";
310 if ( !strcmp(argv[3],"-r") )
312 int Nb = Draw::Atoi(argv[4]);
313 Handle(TColStd_HArray1OfReal) rArr = new TColStd_HArray1OfReal(1,Nb);
314 for ( int i = 1; i <= Nb; i++ )
315 rArr->SetValue(i, Draw::Atof(argv[4+i]));
316 tObj->SetRealArr( rArr );
319 tObj->SetInt( Draw::Atoi(argv[3] ) );
324 //=======================================================================
327 //=======================================================================
328 static Standard_Integer getVal (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
330 if (argc < 4) {di<<"Use "<< argv[0] << "DocName ObjName1 -i | -r\n";return 1;}
332 Handle(TObjDRAW_Object) tObj = getObjByName( argv[1], argv[2] );
335 di << "Error: Object " << argv[2] << " not found" << "\n";
338 if ( !strcmp(argv[3],"-i") )
339 di << tObj->GetInt();
342 Handle(TColStd_HArray1OfReal) rArr = tObj->GetRealArr();
343 if ( !rArr.IsNull() )
344 for ( int i = 1, n = rArr->Upper(); i <= n; i++ )
348 di << rArr->Value(i);
355 //=======================================================================
358 //=======================================================================
359 static Standard_Integer setRef (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
361 if (argc < 4) {di<<"Use "<< argv[0] << "DocName ObjName1 ObjName2\n";return 1;}
363 Handle(TObjDRAW_Object) tObj1 = getObjByName( argv[1], argv[2] );
364 Handle(TObjDRAW_Object) tObj2 = getObjByName( argv[1], argv[3] );
365 if ( tObj1.IsNull() || tObj2.IsNull() )
367 di << "Error: Object " << argv[2] << " or object " << argv[3] << " not found" << "\n";
370 tObj1->SetRef( tObj2 );
375 //=======================================================================
378 //=======================================================================
379 static Standard_Integer getRef (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
381 if (argc < 3) {di<<"Use "<< argv[0] << "DocName ObjName\n";return 1;}
383 Handle(TObjDRAW_Object) tObj = getObjByName( argv[1], argv[2] );
386 di << "Error: Object " << argv[2] << " not found" << "\n";
389 Handle(TObj_Object) aRefObj = tObj->GetRef();
390 if ( aRefObj.IsNull() )
394 TCollection_AsciiString aName;
395 aRefObj->GetName( aName );
396 di << aName.ToCString();
402 //=======================================================================
403 //function : addChild
405 //=======================================================================
406 static Standard_Integer addChild (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
408 if (argc < 4) {di<<"Use "<< argv[0] << "DocName ObjName childObj\n";return 1;}
410 Handle(TObjDRAW_Object) tObj = getObjByName( argv[1], argv[2] );
413 di << "Error: Object " << argv[2] << " not found" << "\n";
416 Handle(TObj_Object) chldObj = tObj->AddChild();
417 if ( chldObj.IsNull() )
419 di << "Error: No child object created" << "\n";
422 chldObj->SetName( new TCollection_HExtendedString( argv[3] ) );
427 //=======================================================================
428 //function : getChild
430 //=======================================================================
431 static Standard_Integer getChild (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
433 if (argc < 3) {di<<"Use "<< argv[0] << "DocName ObjName\n";return 1;}
435 Handle(TObjDRAW_Object) tObj = getObjByName( argv[1], argv[2] );
438 di << "Error: Object " << argv[2] << " not found" << "\n";
441 Handle(TObj_ObjectIterator) anItr = tObj->GetChildren();
443 for ( ; anItr->More(); anItr->Next(), i++ )
445 Handle(TObj_Object) anObj = anItr->Value();
446 TCollection_AsciiString aName;
447 anObj->GetName( aName );
450 di << aName.ToCString();
456 //=======================================================================
459 //=======================================================================
461 void TObjDRAW::Init(Draw_Interpretor& di)
464 static Standard_Boolean initactor = Standard_False;
465 if (initactor) return; initactor = Standard_True;
467 // load TObjOcaf base data model messages
468 Message_MsgFile::Load( ::getenv( "CSF_TObjResources" ), "TObj.msg" );
470 //=====================================
472 //=====================================
474 Standard_CString g = "TObj general commands";
476 di.Add ("TObjNew","DocName \t: Create new TObj model with document named DocName",
477 __FILE__, newModel, g);
479 di.Add ("TObjSave","DocName [Path] \t: Save Model with DocName",
480 __FILE__, saveModel, g);
482 di.Add ("TObjLoad","DocName Path \t: Load model DocName from file Path",
483 __FILE__, loadModel, g);
485 di.Add ("TObjClose","DocName\t: Close model DocName",
486 __FILE__, closeModel, g);
488 di.Add ("TObjAddObj","DocName ObjName \t: Add object to model document",
489 __FILE__, addObj, g);
491 di.Add ("TObjSetVal","DocName ObjName1 intVal | -r N r1 r2 ... rN \t: Set one integer or set of real values",
492 __FILE__, setVal, g);
494 di.Add ("TObjGetVal","DocName ObjName1 -i | -r \t: Returns one integer or set of real values",
495 __FILE__, getVal, g);
497 di.Add ("TObjSetRef","DocName ObjName1 ObjName2 \t: Set reference from object1 to object2",
498 __FILE__, setRef, g);
500 di.Add ("TObjGetRef","DocName ObjName \t: Returns list of children objects",
501 __FILE__, getRef, g);
503 di.Add ("TObjAddChild","DocName ObjName chldName \t: Add child object to indicated object",
504 __FILE__, addChild, g);
506 di.Add ("TObjGetChildren","DocName ObjName \t: Returns list of children objects",
507 __FILE__, getChild, g);
512 //==============================================================================
515 //==============================================================================
516 void TObjDRAW::Factory(Draw_Interpretor& theDI)
518 TObjDRAW::Init(theDI);
521 theDI << "Draw Plugin : All TKTObjDRAW commands are loaded" << "\n";
525 // Declare entry point PLUGINFACTORY