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