ROOT  6.06/09
Reference Guide
TOCCToStep.cxx
Go to the documentation of this file.
1 // @(#)geom/geocad:$Id$
2 // Author: Cinzia Luzzi 5/5/2012
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2012, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 //////////////////////////////////////////////////////////////////////////
13 // TOCCToStep Class //
14 // //
15 // This class contains implementation of writing OpenCascade's //
16 // geometry shapes to the STEP file reproducing the originary ROOT //
17 // geometry tree. The TRootStep Class takes a gGeoManager pointer and //
18 // gives back a STEP file. //
19 // The OCCShapeCreation(TGeoManager *m) method starting from //
20 // the top of the ROOT geometry tree translates each ROOT shape in the //
21 // OCC one. A fLabel is created for each OCC shape and the //
22 // correspondance bewteen the the fLabel and the shape is saved //
23 // in a map. The OCCTreeCreation(TGeoManager *m) method starting from //
24 // the top of the ROOT geometry and using the fLabel-shape map //
25 // reproduce the ROOT tree that will be written to the STEP file using //
26 // the OCCWriteStep(const char * fname ) method. //
27 // //
28 //////////////////////////////////////////////////////////////////////////
29 
30 #include "TOCCToStep.h"
31 #include "TGeoToOCC.h"
32 
33 #include "TGeoVolume.h"
34 #include "TString.h"
35 #include "TClass.h"
36 #include "TGeoManager.h"
37 #include "TError.h"
38 
39 #include <Interface_Static.hxx>
40 #include <BRepBuilderAPI_Transform.hxx>
41 #include <TDataStd_Name.hxx>
42 #include <XCAFDoc_DocumentTool.hxx>
43 #include <Standard.hxx>
44 #include <stdlib.h>
45 #include <XCAFApp_Application.hxx>
46 
47 using namespace std;
48 
49 
50 ////////////////////////////////////////////////////////////////////////////////
51 
53 {
54  OCCDocCreation();
55 }
56 
57 ////////////////////////////////////////////////////////////////////////////////
58 
60 {
61  Handle (XCAFApp_Application)A = XCAFApp_Application::GetApplication();
62  if (!A.IsNull()) {
63  A->NewDocument ("MDTV-XCAF", fDoc);
64  }
65  else
66  ::Error("TOCCToStep::OCCDocCreation", "creating OCC application");
67 }
68 
69 ////////////////////////////////////////////////////////////////////////////////
70 /// Logical fTree creation.
71 
73 {
74  TDF_Label motherLabel;
75  TGeoVolume * currentVolume;
76  TGeoVolume * motherVol;
77  TGeoVolume * Top;
78  TString path;
79  Int_t num = 0;
80  Int_t level = 0;
82  fLabel = XCAFDoc_DocumentTool::ShapeTool(fDoc->Main())->NewShape();
83  fShape = fRootShape.OCC_SimpleShape(m->GetTopVolume()->GetShape());
84  XCAFDoc_DocumentTool::ShapeTool(fDoc->Main())->SetShape(fLabel, fShape);
85  TDataStd_Name::Set(fLabel, m->GetTopVolume()->GetName());
86  XCAFDoc_DocumentTool::ShapeTool(fDoc->Main())->UpdateAssembly(fDoc->Main());
87  Top = m->GetTopVolume();
88  fTree[Top] = fLabel;
89  while ((currentVolume = (TGeoVolume *)next())) {
90  if (GetLabelOfVolume(currentVolume).IsNull()) {
91  num = currentVolume->GetNdaughters();
92  if ((GetLabelOfVolume(currentVolume).IsNull())) {
93  if (currentVolume->GetShape()->IsA()==TGeoCompositeShape::Class()) {
94  fShape = fRootShape.OCC_CompositeShape((TGeoCompositeShape*)currentVolume->GetShape(), TGeoIdentity());
95  } else {
96  fShape = fRootShape.OCC_SimpleShape(currentVolume->GetShape());
97  }
98  }
99  TGeoNode *current;
100  TGeoIterator nextNode(m->GetTopVolume());
101  while ((current = nextNode())) {
102  if ((current->GetVolume() == currentVolume) && (GetLabelOfVolume(current->GetVolume()).IsNull())) {
103  level = nextNode.GetLevel();
104  nextNode.GetPath(path);
105  if (level == 1)
106  motherVol = m->GetTopVolume();
107  else {
108  TGeoNode * mother = nextNode.GetNode(--level);
109  motherVol = mother->GetVolume();
110  }
111  motherLabel = GetLabelOfVolume(motherVol);
112  if (!motherLabel.IsNull()) {
113  fLabel = TDF_TagSource::NewChild(motherLabel);
114  break;
115  } else {
116  TGeoNode * grandMother = nextNode.GetNode(level);
117  motherVol = grandMother->GetVolume();
118  TopoDS_Shape Mothershape;
119  if (motherVol->GetShape()->IsA()==TGeoCompositeShape::Class()) {
120  Mothershape = fRootShape.OCC_CompositeShape((TGeoCompositeShape*)motherVol->GetShape(), TGeoIdentity());
121  } else {
122  Mothershape = fRootShape.OCC_SimpleShape(motherVol->GetShape());
123  }
124  motherLabel = TDF_TagSource::NewChild(GetLabelOfVolume(Top));
125  XCAFDoc_DocumentTool::ShapeTool(fDoc->Main())->SetShape(motherLabel, Mothershape);
126  TDataStd_Name::Set(motherLabel, motherVol->GetName());
127  XCAFDoc_DocumentTool::ShapeTool(fDoc->Main())->UpdateAssembly(fDoc->Main());
128  fTree[motherVol] = motherLabel;
129  fLabel = TDF_TagSource::NewChild(motherLabel);
130  break;
131  }
132  }
133  }
134  XCAFDoc_DocumentTool::ShapeTool(fDoc->Main())->SetShape(fLabel, fShape);
135  TDataStd_Name::Set(fLabel, currentVolume->GetName());
136  XCAFDoc_DocumentTool::ShapeTool(fDoc->Main())->UpdateAssembly(fDoc->Main());
137  fTree[currentVolume] = fLabel;
138  }
139  }
140  return fLabel;
141 }
142 
143 ////////////////////////////////////////////////////////////////////////////////
144 
145 void TOCCToStep::OCCWriteStep(const char *fname)
146 {
147  STEPControl_StepModelType mode = STEPControl_AsIs;
148  fWriter.SetNameMode(Standard_True);
149  if (!Interface_Static::SetIVal("write.step.assembly", 1)) { //assembly mode
150  Error("TOCCToStep::OCCWriteStep", "failed to set assembly mode for step data");
151  }
152  if (!fWriter.Transfer(fDoc, mode)) {
153  ::Error("TOCCToStep::OCCWriteStep", "error translating document");
154  }
155  fWriter.Write(fname);
156 }
157 
158 ////////////////////////////////////////////////////////////////////////////////
159 
161 {
162  TDF_Label null;
163  if (fTree.find(v) != fTree.end())
164  return fTree[v];
165  else
166  return null;
167 }
168 
169 ////////////////////////////////////////////////////////////////////////////////
170 
172 {
173  map <TGeoVolume *,TDF_Label>::iterator it;
174  for(it = fTree.begin(); it != fTree.end(); it++)
175  if (it->second.IsEqual(fLabel))
176  return it->first;
177  return 0;
178 }
179 
180 ////////////////////////////////////////////////////////////////////////////////
181 
182 void TOCCToStep::AddChildLabel(TDF_Label mother, TDF_Label child, TopLoc_Location loc)
183 {
184  XCAFDoc_DocumentTool::ShapeTool(mother)->AddComponent(mother, child,loc);
185  XCAFDoc_DocumentTool::ShapeTool(mother)->UpdateAssembly(mother);
186 }
187 
188 ////////////////////////////////////////////////////////////////////////////////
189 
190 TopLoc_Location TOCCToStep::CalcLocation (TGeoHMatrix matrix)
191 {
192  gp_Trsf TR,TR1;
193  TopLoc_Location locA;
194  Double_t const *t=matrix.GetTranslation();
195  Double_t const *r=matrix.GetRotationMatrix();
196  TR1.SetTranslation(gp_Vec(t[0],t[1],t[2]));
197  TR.SetValues(r[0],r[1],r[2],0,
198  r[3],r[4],r[5],0,
199  r[6],r[7],r[8],0
200 #if OCC_VERSION_MAJOR == 6 && OCC_VERSION_MINOR < 8
201  ,0,1
202 #endif
203  );
204  TR1.Multiply(TR);
205  locA = TopLoc_Location (TR1);
206  return locA;
207 }
208 
209 ////////////////////////////////////////////////////////////////////////////////
210 
212 {
213  TGeoIterator nextNode(m->GetTopVolume());
214  TGeoNode *currentNode = 0;
215  TGeoNode *motherNode = 0;
216  //TGeoNode *gmotherNode = 0;
217  Int_t level;
218  TDF_Label labelMother;
219  TopLoc_Location loc;
220  Int_t nd;
221 
222  while ((currentNode = nextNode())) {
223  level = nextNode.GetLevel();
224  nd = currentNode->GetNdaughters();
225  if (!nd) {
226  for (int i = level; i > 0; i--) {
227  if (i == 1) {
228  motherNode = m->GetTopNode();
229  } else
230  motherNode = nextNode.GetNode(--level);
231  labelMother = GetLabelOfVolume(motherNode->GetVolume());
232  Int_t ndMother = motherNode->GetNdaughters();
233  fLabel = GetLabelOfVolume(currentNode->GetVolume());
234  loc = CalcLocation((*(currentNode->GetMatrix())));
235  if ((XCAFDoc_DocumentTool::ShapeTool(fDoc->Main())->NbComponents(labelMother) < ndMother) && (!nd)) {
236  AddChildLabel(labelMother, fLabel, loc);
237  } else if ((XCAFDoc_DocumentTool::ShapeTool(fDoc->Main())->NbComponents(fLabel) == nd) && (XCAFDoc_DocumentTool::ShapeTool(fDoc->Main())->NbComponents(labelMother) == motherNode->GetVolume()->GetIndex(currentNode))) {
238  AddChildLabel(labelMother, fLabel, loc);
239  }
240  currentNode = motherNode;
241  fLabel = labelMother;
242  nd = currentNode->GetNdaughters();
243  }
244  }
245  }
246 }
247 
248 ////////////////////////////////////////////////////////////////////////////////
249 
251 {
252 #if OCC_VERSION_MAJOR == 6 && OCC_VERSION_MINOR < 8
253  XCAFDoc_DocumentTool::ShapeTool(fDoc->Main())->Dump();
254 #else
255  XCAFDoc_DocumentTool::ShapeTool(fDoc->Main())->Dump(std::cout);
256 #endif
257 }
258 
259 
TGeoVolume * GetVolume() const
Definition: TGeoNode.h:106
void OCCWriteStep(const char *fname)
Definition: TOCCToStep.cxx:145
ClassImp(TSeqCollection) Int_t TSeqCollection TIter next(this)
Return index of object in collection.
void AddChildLabel(TDF_Label mother, TDF_Label child, TopLoc_Location loc)
Definition: TOCCToStep.cxx:182
TGeoVolume * GetVolumeOfLabel(TDF_Label fLabel)
Definition: TOCCToStep.cxx:171
Basic string class.
Definition: TString.h:137
int Int_t
Definition: RtypesCore.h:41
STL namespace.
void PrintAssembly()
Definition: TOCCToStep.cxx:250
static double A[]
Int_t GetNdaughters() const
Definition: TGeoVolume.h:362
null_t< F > null()
void Class()
Definition: Class.C:29
TGeoNode * GetTopNode() const
Definition: TGeoManager.h:500
virtual const Double_t * GetRotationMatrix() const
Definition: TGeoMatrix.h:456
void OCCTreeCreation(TGeoManager *m)
Definition: TOCCToStep.cxx:211
TGeoVolume * GetTopVolume() const
Definition: TGeoManager.h:499
void Error(const char *location, const char *msgfmt,...)
TObjArray * GetListOfVolumes() const
Definition: TGeoManager.h:465
Int_t GetIndex(const TGeoNode *node) const
get index number for a given daughter
Int_t GetLevel() const
Definition: TGeoNode.h:286
ROOT::R::TRInterface & r
Definition: Object.C:4
SVector< double, 2 > v
Definition: Dict.h:5
TMarker * m
Definition: textangle.C:8
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:51
#define TR(N1, N2)
void OCCDocCreation()
Definition: TOCCToStep.cxx:59
TopLoc_Location CalcLocation(TGeoHMatrix matrix)
Definition: TOCCToStep.cxx:190
TGeoShape * GetShape() const
Definition: TGeoVolume.h:204
double Double_t
Definition: RtypesCore.h:55
TDF_Label OCCShapeCreation(TGeoManager *m)
Logical fTree creation.
Definition: TOCCToStep.cxx:72
TDF_Label GetLabelOfVolume(TGeoVolume *v)
Definition: TOCCToStep.cxx:160
virtual const Double_t * GetTranslation() const
Definition: TGeoMatrix.h:455
Int_t GetNdaughters() const
Definition: TGeoNode.h:102