b311480e |
1 | // Created on: 2008-06-07 |
2 | // Created by: Pavel TELKOV |
973c2be1 |
3 | // Copyright (c) 2008-2014 OPEN CASCADE SAS |
b311480e |
4 | // |
973c2be1 |
5 | // This file is part of Open CASCADE Technology software library. |
b311480e |
6 | // |
d5f74e42 |
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 |
973c2be1 |
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. |
b311480e |
12 | // |
973c2be1 |
13 | // Alternatively, this file may be used under the terms of Open CASCADE |
14 | // commercial license or contractual agreement. |
7fd59977 |
15 | |
16 | #include <TObjDRAW.ixx> |
17 | #include <stdio.h> |
18 | |
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> |
24 | |
25 | #include <Message_MsgFile.hxx> |
26 | #include <Standard_DefineHandle.hxx> |
27 | #include <TColStd_HArray1OfReal.hxx> |
28 | #include <TCollection_ExtendedString.hxx> |
29 | |
30 | #include <Draw.hxx> |
31 | |
32 | #include <DDocStd.hxx> |
33 | #include <DDocStd_DrawDocument.hxx> |
34 | |
35 | #include <TDF_Tool.hxx> |
36 | #include <TDF_Data.hxx> |
37 | #include <TDataStd_Name.hxx> |
38 | #include <TDocStd_Document.hxx> |
39 | |
40 | #include <Draw_PluginMacro.hxx> |
41 | |
42 | // avoid warnings on 'extern "C"' functions returning C++ classes |
43 | #ifdef WNT |
44 | #pragma warning(4:4190) |
45 | #endif |
46 | |
47 | //======================================================================= |
48 | // Section: General commands |
49 | //======================================================================= |
50 | |
51 | class Handle(TObjDRAW_Model); |
52 | |
53 | //! simple model with redefined pure virtual method |
54 | class TObjDRAW_Model : public TObj_Model |
55 | { |
56 | public: |
57 | Standard_EXPORT TObjDRAW_Model() |
58 | : TObj_Model() {} |
59 | |
60 | virtual Standard_EXPORT Handle(TObj_Model) NewEmpty() |
61 | { |
62 | return new TObjDRAW_Model(); |
63 | } |
64 | |
65 | public: |
66 | //! CASCADE RTTI |
67 | DEFINE_STANDARD_RTTI(TObjDRAW_Model) |
68 | |
69 | }; |
70 | DEFINE_STANDARD_HANDLE (TObjDRAW_Model,TObj_Model) |
71 | |
72 | IMPLEMENT_STANDARD_HANDLE(TObjDRAW_Model,TObj_Model) |
73 | IMPLEMENT_STANDARD_RTTIEXT(TObjDRAW_Model,TObj_Model) |
74 | |
75 | class Handle(TObjDRAW_Object); |
76 | |
77 | //! simple object to check API and features of TObj_Object |
78 | class TObjDRAW_Object : public TObj_Object |
79 | { |
80 | protected: |
81 | //! enumeration for the ranks of label under Data section. |
82 | enum DataTag |
83 | { |
84 | DataTag_First = TObj_Object::DataTag_Last, |
85 | DataTag_IntVal, |
86 | DataTag_RealArr, |
87 | DataTag_Last = DataTag_First + 100 |
88 | }; |
89 | |
90 | // enumeration for the ranks of label under Reference section. |
91 | enum RefTag |
92 | { |
93 | RefTag_First = TObj_Object::RefTag_Last, |
94 | RefTag_Other, //!< here we test only one refrence to other |
95 | RefTag_Last = RefTag_First + 100 |
96 | }; |
97 | |
98 | //! enumeration for the ranks of label under Children section. |
99 | enum ChildTag |
100 | { |
101 | ChildTag_First = TObj_Object::ChildTag_Last, |
102 | ChildTag_Child, //!< here we test only one child (or one branch of children) |
103 | ChildTag_Last = ChildTag_First + 100 |
104 | }; |
105 | |
106 | public: |
107 | Standard_EXPORT TObjDRAW_Object(const TDF_Label& theLab) |
108 | : TObj_Object( theLab ) {} |
109 | |
110 | //! sets int value |
111 | Standard_EXPORT void SetInt( const Standard_Integer theVal ) |
112 | { setInteger( theVal, DataTag_IntVal ); } |
113 | //! returns int value |
114 | Standard_EXPORT Standard_Integer GetInt() const |
115 | { return getInteger( DataTag_IntVal ); } |
116 | |
117 | //! sets array of real |
118 | Standard_EXPORT void SetRealArr( const Handle(TColStd_HArray1OfReal)& theHArr ) |
119 | { setArray( theHArr, DataTag_RealArr ); } |
120 | //! returns array of real |
121 | Standard_EXPORT Handle(TColStd_HArray1OfReal) GetRealArr() const |
122 | { return getRealArray( 0, DataTag_RealArr ); } |
123 | |
124 | //! set reference to other object |
125 | Standard_EXPORT void SetRef( const Handle(TObj_Object)& theOther ) |
126 | { setReference( theOther, RefTag_Other ); } |
127 | //! return reference |
128 | Standard_EXPORT Handle(TObj_Object) GetRef() const |
129 | { return getReference( RefTag_Other ); } |
130 | |
131 | //! add child object |
132 | Standard_EXPORT Handle(TObj_Object) AddChild() |
133 | { |
134 | TDF_Label aChL = getChildLabel( ChildTag_Child ).NewChild(); |
135 | return new TObjDRAW_Object( aChL ); |
136 | } |
137 | |
138 | protected: |
139 | // Persistence of TObj object |
140 | DECLARE_TOBJOCAF_PERSISTENCE(TObjDRAW_Object,TObj_Object) |
141 | |
142 | public: |
143 | // Declaration of CASCADE RTTI |
144 | DEFINE_STANDARD_RTTI (TObjDRAW_Object) |
145 | |
146 | }; |
147 | |
148 | // Definition of HANDLE object using Standard_DefineHandle.hxx |
149 | DEFINE_STANDARD_HANDLE (TObjDRAW_Object,TObj_Object) |
150 | |
151 | IMPLEMENT_STANDARD_HANDLE (TObjDRAW_Object,TObj_Object) |
152 | IMPLEMENT_STANDARD_RTTIEXT(TObjDRAW_Object,TObj_Object) |
153 | IMPLEMENT_TOBJOCAF_PERSISTENCE(TObjDRAW_Object) |
154 | |
155 | //======================================================================= |
156 | //function : newModel |
157 | //purpose : |
158 | //======================================================================= |
159 | static Standard_Integer newModel (Draw_Interpretor& di, Standard_Integer argc, const char** argv) |
160 | { |
161 | if (argc < 2) {di<<"Use "<< argv[0] << "nameDoc\n";return 1;} |
162 | |
163 | Handle(TDocStd_Document) D; |
164 | Handle(DDocStd_DrawDocument) DD; |
165 | |
166 | if (!DDocStd::GetDocument(argv[1],D,Standard_False)) { |
167 | Handle(TObjDRAW_Model) aModel = new TObjDRAW_Model(); |
168 | aModel->Load(0); |
169 | D = aModel->GetDocument(); |
170 | DD = new DDocStd_DrawDocument(D); |
171 | TDataStd_Name::Set(D->GetData()->Root(),argv[1]); |
172 | Draw::Set(argv[1],DD); |
173 | di << "document " << argv[1] << " created" << "\n"; |
174 | } |
175 | else di << argv[1] << " is already a document" << "\n"; |
176 | |
177 | return 0; |
178 | } |
179 | |
180 | static Handle(TObj_Model) getModelByName( const char* theName ) |
181 | { |
182 | Handle(TObj_Model) aModel; |
183 | Handle(TDocStd_Document) D; |
184 | if (!DDocStd::GetDocument(theName,D)) return aModel; |
185 | |
186 | TDF_Label aLabel = D->Main(); |
187 | Handle(TObj_TModel) aModelAttr; |
188 | if (!aLabel.IsNull() && aLabel.FindAttribute(TObj_TModel::GetID(), aModelAttr)) |
189 | aModel = aModelAttr->Model(); |
190 | return aModel; |
191 | } |
192 | |
193 | //======================================================================= |
194 | //function : saveModel |
195 | //purpose : |
196 | //======================================================================= |
197 | static Standard_Integer saveModel (Draw_Interpretor& di, Standard_Integer argc, const char** argv) |
198 | { |
199 | if (argc < 2) {di<<"Use "<< argv[0] << "nameDoc [fileName]\n";return 1;} |
200 | |
201 | Handle(TObj_Model) aModel = getModelByName(argv[1]); |
202 | if ( aModel.IsNull() ) return 1; |
203 | Standard_Boolean isSaved = Standard_False; |
204 | if (argc > 2 ) |
205 | isSaved = aModel->SaveAs( argv[2] ); |
206 | else |
207 | isSaved = aModel->Save(); |
208 | |
209 | if (!isSaved) { |
210 | di << "Error: Document not saved" << "\n"; |
211 | return 1; |
212 | } |
213 | return 0; |
214 | } |
215 | |
216 | //======================================================================= |
217 | //function : loadModel |
218 | //purpose : |
219 | //======================================================================= |
220 | static Standard_Integer loadModel (Draw_Interpretor& di, Standard_Integer argc, const char** argv) |
221 | { |
222 | if (argc < 3) {di<<"Use "<< argv[0] << "nameDoc fileName\n";return 1;} |
223 | |
224 | Standard_Boolean isLoaded = Standard_False; |
225 | Handle(TObj_Model) aModel = getModelByName(argv[1]); |
226 | if ( aModel.IsNull() ) |
227 | { |
228 | // create new |
229 | aModel = new TObjDRAW_Model(); |
230 | isLoaded = aModel->Load( argv[2] ); |
231 | if ( isLoaded ) |
232 | { |
233 | Handle(TDocStd_Document) D = aModel->GetDocument(); |
234 | Handle(DDocStd_DrawDocument) DD = new DDocStd_DrawDocument(D); |
235 | |
236 | TDataStd_Name::Set(D->GetData()->Root(),argv[1]); |
237 | Draw::Set(argv[1],DD); |
238 | } |
239 | } |
240 | else |
241 | isLoaded = aModel->Load( argv[2] ); |
242 | |
243 | |
244 | if (!isLoaded) { |
245 | di << "Error: Document not loaded" << "\n"; |
246 | return 1; |
247 | } |
248 | return 0; |
249 | } |
250 | |
251 | |
252 | //======================================================================= |
253 | //function : closeModel |
254 | //purpose : |
255 | //======================================================================= |
256 | static Standard_Integer closeModel (Draw_Interpretor& di, Standard_Integer argc, const char** argv) |
257 | { |
258 | if (argc < 2) {di<<"Use "<< argv[0] << "nameDoc\n";return 1;} |
259 | |
260 | Handle(TObj_Model) aModel = getModelByName(argv[1]); |
261 | if ( aModel.IsNull() ) return 1; |
262 | aModel->Close(); |
263 | |
264 | return 0; |
265 | } |
266 | |
267 | //======================================================================= |
268 | //function : addObj |
269 | //purpose : |
270 | //======================================================================= |
271 | static Standard_Integer addObj (Draw_Interpretor& di, Standard_Integer argc, const char** argv) |
272 | { |
273 | if (argc < 3) {di<<"Use "<< argv[0] << "DocName ObjName\n";return 1;} |
274 | Handle(TObj_Model) aModel = getModelByName(argv[1]); |
275 | if ( aModel.IsNull() ) return 1; |
276 | Handle(TObjDRAW_Object) tObj = |
277 | new TObjDRAW_Object( aModel->GetMainPartition()->NewLabel() ); |
278 | if ( tObj.IsNull() ) |
279 | { |
280 | di << "Error: Object not created" << "\n"; |
281 | return 1; |
282 | } |
283 | tObj->SetName( argv[2] ); |
284 | |
285 | return 0; |
286 | } |
287 | |
288 | static Handle(TObjDRAW_Object) getObjByName( const char* modelName, const char* objName ) |
289 | { |
290 | Handle(TObjDRAW_Object) tObj; |
291 | Handle(TObj_Model) aModel = getModelByName(modelName); |
292 | if ( aModel.IsNull() ) |
293 | return tObj; |
294 | Handle(TCollection_HExtendedString) aName = new TCollection_HExtendedString( objName ); |
295 | Handle(TObj_TNameContainer) aDict; |
296 | tObj = Handle(TObjDRAW_Object)::DownCast( aModel->FindObject(aName, aDict) ); |
297 | return tObj; |
298 | } |
299 | |
300 | //======================================================================= |
301 | //function : setVal |
302 | //purpose : |
303 | //======================================================================= |
304 | static Standard_Integer setVal (Draw_Interpretor& di, Standard_Integer argc, const char** argv) |
305 | { |
306 | if (argc < 4) {di<<"Use "<< argv[0] << "DocName ObjName1 intVal | -r N r1 r2 ... rN\n";return 1;} |
307 | Handle(TObjDRAW_Object) tObj = getObjByName( argv[1], argv[2] ); |
308 | if ( tObj.IsNull() ) |
309 | { |
310 | di << "Error: Object " << argv[2] << " not found" << "\n"; |
311 | return 1; |
312 | } |
313 | if ( !strcmp(argv[3],"-r") ) |
314 | { |
91322f44 |
315 | int Nb = Draw::Atoi(argv[4]); |
7fd59977 |
316 | Handle(TColStd_HArray1OfReal) rArr = new TColStd_HArray1OfReal(1,Nb); |
317 | for ( int i = 1; i <= Nb; i++ ) |
91322f44 |
318 | rArr->SetValue(i, Draw::Atof(argv[4+i])); |
7fd59977 |
319 | tObj->SetRealArr( rArr ); |
320 | } |
321 | else |
91322f44 |
322 | tObj->SetInt( Draw::Atoi(argv[3] ) ); |
7fd59977 |
323 | |
324 | return 0; |
325 | } |
326 | |
327 | //======================================================================= |
328 | //function : getVal |
329 | //purpose : |
330 | //======================================================================= |
331 | static Standard_Integer getVal (Draw_Interpretor& di, Standard_Integer argc, const char** argv) |
332 | { |
333 | if (argc < 4) {di<<"Use "<< argv[0] << "DocName ObjName1 -i | -r\n";return 1;} |
334 | |
335 | Handle(TObjDRAW_Object) tObj = getObjByName( argv[1], argv[2] ); |
336 | if ( tObj.IsNull() ) |
337 | { |
338 | di << "Error: Object " << argv[2] << " not found" << "\n"; |
339 | return 1; |
340 | } |
341 | if ( !strcmp(argv[3],"-i") ) |
342 | di << tObj->GetInt(); |
343 | else |
344 | { |
345 | Handle(TColStd_HArray1OfReal) rArr = tObj->GetRealArr(); |
346 | if ( !rArr.IsNull() ) |
347 | for ( int i = 1, n = rArr->Upper(); i <= n; i++ ) |
348 | { |
349 | if ( i > 1 ) |
350 | di << " "; |
351 | di << rArr->Value(i); |
352 | } |
353 | } |
354 | |
355 | return 0; |
356 | } |
357 | |
358 | //======================================================================= |
359 | //function : setRef |
360 | //purpose : |
361 | //======================================================================= |
362 | static Standard_Integer setRef (Draw_Interpretor& di, Standard_Integer argc, const char** argv) |
363 | { |
364 | if (argc < 4) {di<<"Use "<< argv[0] << "DocName ObjName1 ObjName2\n";return 1;} |
365 | |
366 | Handle(TObjDRAW_Object) tObj1 = getObjByName( argv[1], argv[2] ); |
367 | Handle(TObjDRAW_Object) tObj2 = getObjByName( argv[1], argv[3] ); |
368 | if ( tObj1.IsNull() || tObj2.IsNull() ) |
369 | { |
370 | di << "Error: Object " << argv[2] << " or object " << argv[3] << " not found" << "\n"; |
371 | return 1; |
372 | } |
373 | tObj1->SetRef( tObj2 ); |
374 | |
375 | return 0; |
376 | } |
377 | |
378 | //======================================================================= |
379 | //function : getRef |
380 | //purpose : |
381 | //======================================================================= |
382 | static Standard_Integer getRef (Draw_Interpretor& di, Standard_Integer argc, const char** argv) |
383 | { |
384 | if (argc < 3) {di<<"Use "<< argv[0] << "DocName ObjName\n";return 1;} |
385 | |
386 | Handle(TObjDRAW_Object) tObj = getObjByName( argv[1], argv[2] ); |
387 | if ( tObj.IsNull() ) |
388 | { |
389 | di << "Error: Object " << argv[2] << " not found" << "\n"; |
390 | return 1; |
391 | } |
392 | Handle(TObj_Object) aRefObj = tObj->GetRef(); |
393 | if ( aRefObj.IsNull() ) |
394 | return 1; |
395 | else |
396 | { |
397 | TCollection_AsciiString aName; |
398 | aRefObj->GetName( aName ); |
399 | di << aName.ToCString(); |
400 | } |
401 | |
402 | return 0; |
403 | } |
404 | |
405 | //======================================================================= |
406 | //function : addChild |
407 | //purpose : |
408 | //======================================================================= |
409 | static Standard_Integer addChild (Draw_Interpretor& di, Standard_Integer argc, const char** argv) |
410 | { |
411 | if (argc < 4) {di<<"Use "<< argv[0] << "DocName ObjName childObj\n";return 1;} |
412 | |
413 | Handle(TObjDRAW_Object) tObj = getObjByName( argv[1], argv[2] ); |
414 | if ( tObj.IsNull() ) |
415 | { |
416 | di << "Error: Object " << argv[2] << " not found" << "\n"; |
417 | return 1; |
418 | } |
419 | Handle(TObj_Object) chldObj = tObj->AddChild(); |
420 | if ( chldObj.IsNull() ) |
421 | { |
422 | di << "Error: No child object created" << "\n"; |
423 | return 1; |
424 | } |
425 | chldObj->SetName( new TCollection_HExtendedString( argv[3] ) ); |
426 | |
427 | return 0; |
428 | } |
429 | |
430 | //======================================================================= |
431 | //function : getChild |
432 | //purpose : |
433 | //======================================================================= |
434 | static Standard_Integer getChild (Draw_Interpretor& di, Standard_Integer argc, const char** argv) |
435 | { |
436 | if (argc < 3) {di<<"Use "<< argv[0] << "DocName ObjName\n";return 1;} |
437 | |
438 | Handle(TObjDRAW_Object) tObj = getObjByName( argv[1], argv[2] ); |
439 | if ( tObj.IsNull() ) |
440 | { |
441 | di << "Error: Object " << argv[2] << " not found" << "\n"; |
442 | return 1; |
443 | } |
444 | Handle(TObj_ObjectIterator) anItr = tObj->GetChildren(); |
445 | int i = 0; |
446 | for ( ; anItr->More(); anItr->Next(), i++ ) |
447 | { |
448 | Handle(TObj_Object) anObj = anItr->Value(); |
449 | TCollection_AsciiString aName; |
450 | anObj->GetName( aName ); |
451 | if ( i > 0 ) |
452 | di << " "; |
453 | di << aName.ToCString(); |
454 | } |
455 | |
456 | return 0; |
457 | } |
458 | |
459 | //======================================================================= |
460 | //function : Init |
461 | //purpose : |
462 | //======================================================================= |
463 | |
464 | void TObjDRAW::Init(Draw_Interpretor& di) |
465 | { |
466 | |
467 | static Standard_Boolean initactor = Standard_False; |
468 | if (initactor) return; initactor = Standard_True; |
469 | |
470 | // load TObjOcaf base data model messages |
471 | Message_MsgFile::Load( ::getenv( "CSF_TObjResources" ), "TObj.msg" ); |
472 | |
473 | //===================================== |
474 | // General commands |
475 | //===================================== |
476 | |
477 | Standard_CString g = "TObj general commands"; |
478 | |
479 | di.Add ("TObjNew","DocName \t: Create new TObj model with document named DocName", |
480 | __FILE__, newModel, g); |
481 | |
482 | di.Add ("TObjSave","DocName [Path] \t: Save Model with DocName", |
483 | __FILE__, saveModel, g); |
484 | |
485 | di.Add ("TObjLoad","DocName Path \t: Load model DocName from file Path", |
486 | __FILE__, loadModel, g); |
487 | |
488 | di.Add ("TObjClose","DocName\t: Close model DocName", |
489 | __FILE__, closeModel, g); |
490 | |
491 | di.Add ("TObjAddObj","DocName ObjName \t: Add object to model document", |
492 | __FILE__, addObj, g); |
493 | |
494 | di.Add ("TObjSetVal","DocName ObjName1 intVal | -r N r1 r2 ... rN \t: Set one integer or set of real values", |
495 | __FILE__, setVal, g); |
496 | |
497 | di.Add ("TObjGetVal","DocName ObjName1 -i | -r \t: Returns one integer or set of real values", |
498 | __FILE__, getVal, g); |
499 | |
500 | di.Add ("TObjSetRef","DocName ObjName1 ObjName2 \t: Set reference from object1 to object2", |
501 | __FILE__, setRef, g); |
502 | |
503 | di.Add ("TObjGetRef","DocName ObjName \t: Returns list of children objects", |
504 | __FILE__, getRef, g); |
505 | |
506 | di.Add ("TObjAddChild","DocName ObjName chldName \t: Add child object to indicated object", |
507 | __FILE__, addChild, g); |
508 | |
509 | di.Add ("TObjGetChildren","DocName ObjName \t: Returns list of children objects", |
510 | __FILE__, getChild, g); |
511 | |
512 | } |
513 | |
514 | |
515 | //============================================================================== |
516 | // TObjDRAW::Factory |
517 | |
518 | //============================================================================== |
519 | void TObjDRAW::Factory(Draw_Interpretor& theDI) |
520 | { |
521 | TObjDRAW::Init(theDI); |
522 | |
0797d9d3 |
523 | #ifdef OCCT_DEBUG |
7fd59977 |
524 | theDI << "Draw Plugin : All TKTObjDRAW commands are loaded" << "\n"; |
525 | #endif |
526 | } |
527 | |
528 | // Declare entry point PLUGINFACTORY |
529 | DPLUGIN(TObjDRAW) |