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