1 // Created on: 2000-06-15
2 // Created by: Edward AGAPOV
3 // Copyright (c) 2000-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 #ifndef _XCAFDoc_ShapeTool_HeaderFile
17 #define _XCAFDoc_ShapeTool_HeaderFile
19 #include <Standard.hxx>
20 #include <Standard_Type.hxx>
22 #include <XCAFDoc_DataMapOfShapeLabel.hxx>
23 #include <Standard_Boolean.hxx>
24 #include <TDataStd_NamedData.hxx>
25 #include <TDF_Attribute.hxx>
26 #include <TDF_LabelSequence.hxx>
27 #include <Standard_Integer.hxx>
28 #include <Standard_OStream.hxx>
29 #include <TColStd_SequenceOfHAsciiString.hxx>
30 #include <TDF_AttributeSequence.hxx>
31 #include <TopTools_SequenceOfShape.hxx>
35 class TopLoc_Location;
37 class TDF_RelocationTable;
38 class XCAFDoc_GraphNode;
41 class XCAFDoc_ShapeTool;
42 DEFINE_STANDARD_HANDLE(XCAFDoc_ShapeTool, TDF_Attribute)
44 //! A tool to store shapes in an XDE
45 //! document in the form of assembly structure, and to maintain this structure.
46 //! Attribute containing Shapes section of DECAF document.
47 //! Provide tools for management of Shapes section.
48 //! The API provided by this class allows to work with this
49 //! structure regardless of its low-level implementation.
50 //! All the shapes are stored on child labels of a main label which is
51 //! XCAFDoc_DocumentTool::LabelShapes(). The label for assembly also has
52 //! sub-labels, each of which represents the instance of
53 //! another shape in that assembly (component). Such sub-label
54 //! stores reference to the label of the original shape in the form
55 //! of TDataStd_TreeNode with GUID XCAFDoc::ShapeRefGUID(), and its
56 //! location encapsulated into the NamedShape.
57 //! For correct work with an XDE document, it is necessary to use
58 //! methods for analysis and methods for working with shapes.
60 //! if ( STool->IsAssembly(aLabel) )
61 //! { Standard_Boolean subchilds = Standard_False; (default)
62 //! Standard_Integer nbc = STool->NbComponents
63 //! (aLabel[,subchilds]);
65 //! If subchilds is True, commands also consider sub-levels. By
66 //! default, only level one is checked.
67 //! In this example, number of children from the first level of
68 //! assembly will be returned. Methods for creation and initialization:
70 //! XCAFDoc_ShapeTool::XCAFDoc_ShapeTool()
72 //! Standard_GUID GetID ();
73 //! Creation (if does not exist) of ShapeTool on label L:
74 //! Handle(XCAFDoc_ShapeTool) XCAFDoc_ShapeTool::Set(const TDF_Label& L)
75 //! Analyze whether shape is a simple shape or an instance or a
76 //! component of an assembly or it is an assembly ( methods of analysis).
78 //! STool->IsShape(aLabel) ;
79 //! Analyze that the label represents a shape (simple
80 //! shape, assembly or reference) or
81 //! STool->IsTopLevel(aLabel);
82 //! Analyze that the label is a label of a top-level shape.
83 //! Work with simple shapes, assemblies and instances (
84 //! methods for work with shapes).
87 //! Standard_Boolean makeAssembly;
88 //! // True to interpret a Compound as an Assembly, False to take it
90 //! aLabel = STool->AddShape(aShape, makeAssembly);
92 //! TDF_Label aLabel...
93 //! // A label must be present if
94 //! (aLabel.IsNull()) { ... no such label : abandon .. }
95 //! TopoDS_Shape aShape;
96 //! aShape = STool->GetShape(aLabel);
97 //! if (aShape.IsNull())
98 //! { ... this label is not for a Shape ... }
99 //! To get a label from shape.
100 //! Standard_Boolean findInstance = Standard_False;
101 //! (this is default value)
102 //! aLabel = STool->FindShape(aShape [,findInstance]);
103 //! if (aLabel.IsNull())
104 //! { ... no label found for this shape ... }
105 class XCAFDoc_ShapeTool : public TDF_Attribute
111 Standard_EXPORT static const Standard_GUID& GetID();
113 //! Create (if not exist) ShapeTool from XCAFDoc on <L>.
114 Standard_EXPORT static Handle(XCAFDoc_ShapeTool) Set (const TDF_Label& L);
116 //! Creates an empty tool
117 //! Creates a tool to work with a document <Doc>
118 //! Attaches to label XCAFDoc::LabelShapes()
119 Standard_EXPORT XCAFDoc_ShapeTool();
121 //! Returns True if the label is a label of top-level shape,
122 //! as opposed to component of assembly or subshape
123 Standard_EXPORT Standard_Boolean IsTopLevel (const TDF_Label& L) const;
125 //! Returns True if the label is not used by any assembly, i.e.
126 //! contains sublabels which are assembly components
127 //! This is relevant only if IsShape() is True
128 //! (There is no Father TreeNode on this <L>)
129 Standard_EXPORT static Standard_Boolean IsFree (const TDF_Label& L);
131 //! Returns True if the label represents a shape (simple shape,
132 //! assembly or reference)
133 Standard_EXPORT static Standard_Boolean IsShape (const TDF_Label& L);
135 //! Returns True if the label is a label of simple shape
136 Standard_EXPORT static Standard_Boolean IsSimpleShape (const TDF_Label& L);
138 //! Return true if <L> is a located instance of other shape
140 Standard_EXPORT static Standard_Boolean IsReference (const TDF_Label& L);
142 //! Returns True if the label is a label of assembly, i.e.
143 //! contains sublabels which are assembly components
144 //! This is relevant only if IsShape() is True
145 Standard_EXPORT static Standard_Boolean IsAssembly (const TDF_Label& L);
147 //! Return true if <L> is reference serving as component
149 Standard_EXPORT static Standard_Boolean IsComponent (const TDF_Label& L);
151 //! Returns True if the label is a label of compound, i.e.
152 //! contains some sublabels
153 //! This is relevant only if IsShape() is True
154 Standard_EXPORT static Standard_Boolean IsCompound (const TDF_Label& L);
156 //! Return true if <L> is subshape of the top-level shape
157 Standard_EXPORT static Standard_Boolean IsSubShape (const TDF_Label& L);
159 //! Checks whether shape <sub> is subshape of shape stored on
161 Standard_EXPORT Standard_Boolean IsSubShape (const TDF_Label& shapeL, const TopoDS_Shape& sub) const;
163 Standard_EXPORT Standard_Boolean SearchUsingMap (const TopoDS_Shape& S, TDF_Label& L, const Standard_Boolean findWithoutLoc, const Standard_Boolean findSubshape) const;
165 //! General tool to find a (sub) shape in the document
166 //! * If findInstance is True, and S has a non-null location,
167 //! first tries to find the shape among the top-level shapes
168 //! with this location
169 //! * If not found, and findComponent is True, tries to find the shape
170 //! among the components of assemblies
171 //! * If not found, tries to find the shape without location
172 //! among top-level shapes
173 //! * If not found and findSubshape is True, tries to find a
174 //! shape as a subshape of top-level simple shapes
175 //! Returns False if nothing is found
176 Standard_EXPORT Standard_Boolean Search (const TopoDS_Shape& S, TDF_Label& L, const Standard_Boolean findInstance = Standard_True, const Standard_Boolean findComponent = Standard_True, const Standard_Boolean findSubshape = Standard_True) const;
178 //! Returns the label corresponding to shape S
179 //! (searches among top-level shapes, not including subcomponents
180 //! of assemblies and subshapes)
181 //! If findInstance is False (default), seach for the
182 //! input shape without location
183 //! If findInstance is True, searches for the
184 //! input shape as is.
185 //! Return True if <S> is found.
186 Standard_EXPORT Standard_Boolean FindShape (const TopoDS_Shape& S, TDF_Label& L, const Standard_Boolean findInstance = Standard_False) const;
188 //! Does the same as previous method
189 //! Returns Null label if not found
190 Standard_EXPORT TDF_Label FindShape (const TopoDS_Shape& S, const Standard_Boolean findInstance = Standard_False) const;
192 //! To get TopoDS_Shape from shape's label
193 //! For component, returns new shape with correct location
194 //! Returns False if label does not contain shape
195 Standard_EXPORT static Standard_Boolean GetShape (const TDF_Label& L, TopoDS_Shape& S);
197 //! To get TopoDS_Shape from shape's label
198 //! For component, returns new shape with correct location
199 //! Returns Null shape if label does not contain shape
200 Standard_EXPORT static TopoDS_Shape GetShape (const TDF_Label& L);
202 //! Creates new (empty) top-level shape.
203 //! Initially it holds empty TopoDS_Compound
204 Standard_EXPORT TDF_Label NewShape() const;
206 //! Sets representation (TopoDS_Shape) for top-level shape.
207 Standard_EXPORT void SetShape (const TDF_Label& L, const TopoDS_Shape& S);
209 //! Adds a new top-level (creates and returns a new label)
210 //! If makeAssembly is True, treats TopAbs_COMPOUND shapes
211 //! as assemblies (creates assembly structure).
212 //! NOTE: <makePrepare> replace components without location
213 //! in assmebly by located components to avoid some problems.
214 //! If AutoNaming() is True then automatically attaches names.
215 Standard_EXPORT TDF_Label AddShape (const TopoDS_Shape& S, const Standard_Boolean makeAssembly = Standard_True, const Standard_Boolean makePrepare = Standard_True);
217 //! Removes shape (whole label and all its sublabels)
218 //! If removeCompletely is true, removes complete shape
219 //! If removeCompletely is false, removes instance(location) only
220 //! Returns False (and does nothing) if shape is not free
221 //! or is not top-level shape
222 Standard_EXPORT Standard_Boolean RemoveShape (const TDF_Label& L, const Standard_Boolean removeCompletely = Standard_True) const;
224 //! set hasComponents into false
225 Standard_EXPORT void Init();
227 //! Sets auto-naming mode to <V>. If True then for added
228 //! shapes, links, assemblies and SHUO's, the TDataStd_Name attribute
229 //! is automatically added. For shapes it contains a shape type
230 //! (e.g. "SOLID", "SHELL", etc); for links it has a form
231 //! "=>[0:1:1:2]" (where a tag is a label containing a shape
232 //! without a location); for assemblies it is "ASSEMBLY", and
233 //! "SHUO" for SHUO's.
234 //! This setting is global; it cannot be made a member function
235 //! as it is used by static methods as well.
236 //! By default, auto-naming is enabled.
237 //! See also AutoNaming().
238 Standard_EXPORT static void SetAutoNaming (const Standard_Boolean V);
240 //! Returns current auto-naming mode. See SetAutoNaming() for
242 Standard_EXPORT static Standard_Boolean AutoNaming();
245 Standard_EXPORT void ComputeShapes (const TDF_Label& L);
247 //! Compute a sequence of simple shapes
248 Standard_EXPORT void ComputeSimpleShapes();
250 //! Returns a sequence of all top-level shapes
251 Standard_EXPORT void GetShapes (TDF_LabelSequence& Labels) const;
253 //! Returns a sequence of all top-level shapes
254 //! which are free (i.e. not referred by any other)
255 Standard_EXPORT void GetFreeShapes (TDF_LabelSequence& FreeLabels) const;
257 //! Returns list of labels which refer shape L as component
258 //! Returns number of users (0 if shape is free)
259 Standard_EXPORT static Standard_Integer GetUsers (const TDF_Label& L, TDF_LabelSequence& Labels, const Standard_Boolean getsubchilds = Standard_False);
261 //! Returns location of instance
262 Standard_EXPORT static TopLoc_Location GetLocation (const TDF_Label& L);
264 //! Returns label which corresponds to a shape referred by L
265 //! Returns False if label is not reference
266 Standard_EXPORT static Standard_Boolean GetReferredShape (const TDF_Label& L, TDF_Label& Label);
268 //! Returns number of Assembles components
269 Standard_EXPORT static Standard_Integer NbComponents (const TDF_Label& L, const Standard_Boolean getsubchilds = Standard_False);
271 //! Returns list of components of assembly
272 //! Returns False if label is not assembly
273 Standard_EXPORT static Standard_Boolean GetComponents (const TDF_Label& L, TDF_LabelSequence& Labels, const Standard_Boolean getsubchilds = Standard_False);
275 //! Adds a component given by its label and location to the assembly
276 //! Note: assembly must be IsAssembly() or IsSimpleShape()
277 Standard_EXPORT TDF_Label AddComponent (const TDF_Label& assembly, const TDF_Label& comp, const TopLoc_Location& Loc);
279 //! Adds a shape (located) as a component to the assembly
280 //! If necessary, creates an additional top-level shape for
281 //! component and return the Label of component.
282 //! If expand is True and component is Compound, it will
283 //! be created as assembly also
284 //! Note: assembly must be IsAssembly() or IsSimpleShape()
285 Standard_EXPORT TDF_Label AddComponent (const TDF_Label& assembly, const TopoDS_Shape& comp, const Standard_Boolean expand = Standard_False);
287 //! Removes a component from its assembly
288 Standard_EXPORT void RemoveComponent (const TDF_Label& comp) const;
290 //! Top-down update for all assembly compounds stored in the document.
291 Standard_EXPORT void UpdateAssemblies();
293 //! Finds a label for subshape <sub> of shape stored on
295 //! Returns Null label if it is not found
296 Standard_EXPORT Standard_Boolean FindSubShape (const TDF_Label& shapeL, const TopoDS_Shape& sub, TDF_Label& L) const;
298 //! Adds a label for subshape <sub> of shape stored on
300 //! Returns Null label if it is not subshape
301 Standard_EXPORT TDF_Label AddSubShape (const TDF_Label& shapeL, const TopoDS_Shape& sub) const;
303 //! Adds (of finds already existed) a label for subshape <sub> of shape stored on
304 //! label shapeL. Label addedSubShapeL returns added (found) label or empty in case of wrong subshape.
305 //! Returns True, if new shape was added, False in case of already existed subshape/wrong subshape
306 Standard_EXPORT Standard_Boolean AddSubShape(const TDF_Label& shapeL, const TopoDS_Shape& sub, TDF_Label& addedSubShapeL) const;
308 Standard_EXPORT TDF_Label FindMainShapeUsingMap (const TopoDS_Shape& sub) const;
310 //! Performs a search among top-level shapes to find
311 //! the shape containing <sub> as subshape
312 //! Checks only simple shapes, and returns the first found
313 //! label (which should be the only one for valid model)
314 Standard_EXPORT TDF_Label FindMainShape (const TopoDS_Shape& sub) const;
316 //! Returns list of labels identifying subshapes of the given shape
317 //! Returns False if no subshapes are placed on that label
318 Standard_EXPORT static Standard_Boolean GetSubShapes (const TDF_Label& L, TDF_LabelSequence& Labels);
320 //! returns the label under which shapes are stored
321 Standard_EXPORT TDF_Label BaseLabel() const;
323 Standard_EXPORT Standard_OStream& Dump (Standard_OStream& theDumpLog, const Standard_Boolean deep) const;
325 Standard_EXPORT virtual Standard_OStream& Dump (Standard_OStream& theDumpLog) const Standard_OVERRIDE;
327 //! Print to std::ostream <theDumpLog> type of shape found on <L> label
328 //! and the entry of <L>, with <level> tabs before.
329 //! If <deep>, print also TShape and Location addresses
330 Standard_EXPORT static void DumpShape (Standard_OStream& theDumpLog, const TDF_Label& L, const Standard_Integer level = 0, const Standard_Boolean deep = Standard_False);
332 Standard_EXPORT const Standard_GUID& ID() const Standard_OVERRIDE;
334 Standard_EXPORT void Restore (const Handle(TDF_Attribute)& with) Standard_OVERRIDE;
336 Standard_EXPORT Handle(TDF_Attribute) NewEmpty() const Standard_OVERRIDE;
338 Standard_EXPORT void Paste (const Handle(TDF_Attribute)& into, const Handle(TDF_RelocationTable)& RT) const Standard_OVERRIDE;
340 //! Returns True if the label is a label of external references, i.e.
341 //! there are some reference on the no-step files, which are
342 //! described in document only their names
343 Standard_EXPORT static Standard_Boolean IsExternRef (const TDF_Label& L);
345 //! Sets the names of references on the no-step files
346 Standard_EXPORT TDF_Label SetExternRefs (const TColStd_SequenceOfHAsciiString& SHAS) const;
348 //! Sets the names of references on the no-step files
349 Standard_EXPORT void SetExternRefs (const TDF_Label& L, const TColStd_SequenceOfHAsciiString& SHAS) const;
351 //! Gets the names of references on the no-step files
352 Standard_EXPORT static void GetExternRefs (const TDF_Label& L, TColStd_SequenceOfHAsciiString& SHAS);
354 //! Sets the SHUO structure between upper_usage and next_usage
355 //! create multy-level (if number of labels > 2) SHUO from first to last
356 //! Initialise out <MainSHUOAttr> by main upper_usage SHUO attribute.
357 //! Returns FALSE if some of labels in not component label
358 Standard_EXPORT Standard_Boolean SetSHUO (const TDF_LabelSequence& Labels, Handle(XCAFDoc_GraphNode)& MainSHUOAttr) const;
360 //! Returns founded SHUO GraphNode attribute <aSHUOAttr>
361 //! Returns false in other case
362 Standard_EXPORT static Standard_Boolean GetSHUO (const TDF_Label& SHUOLabel, Handle(XCAFDoc_GraphNode)& aSHUOAttr);
364 //! Returns founded SHUO GraphNodes of indicated component
365 //! Returns false in other case
366 Standard_EXPORT static Standard_Boolean GetAllComponentSHUO (const TDF_Label& CompLabel, TDF_AttributeSequence& SHUOAttrs);
368 //! Returns the sequence of labels of SHUO attributes,
369 //! which is upper_usage for this next_usage SHUO attribute
370 //! (that indicated by label)
371 //! NOTE: returns upper_usages only on one level (not recurse)
372 //! NOTE: do not clear the sequence before filling
373 Standard_EXPORT static Standard_Boolean GetSHUOUpperUsage (const TDF_Label& NextUsageL, TDF_LabelSequence& Labels);
375 //! Returns the sequence of labels of SHUO attributes,
376 //! which is next_usage for this upper_usage SHUO attribute
377 //! (that indicated by label)
378 //! NOTE: returns next_usages only on one level (not recurse)
379 //! NOTE: do not clear the sequence before filling
380 Standard_EXPORT static Standard_Boolean GetSHUONextUsage (const TDF_Label& UpperUsageL, TDF_LabelSequence& Labels);
382 //! Remove SHUO from component sublabel,
383 //! remove all dependencies on other SHUO.
384 //! Returns FALSE if cannot remove SHUO dependencies.
385 //! NOTE: remove any styles that associated with this SHUO.
386 Standard_EXPORT Standard_Boolean RemoveSHUO (const TDF_Label& SHUOLabel) const;
388 //! Serach the path of labels in the document,
389 //! that corresponds the component from any assembly
390 //! Try to search the sequence of labels with location that
391 //! produce this shape as component of any assembly
392 //! NOTE: Clear sequence of labels before filling
393 Standard_EXPORT Standard_Boolean FindComponent (const TopoDS_Shape& theShape, TDF_LabelSequence& Labels) const;
395 //! Search for the component shape that styled by shuo
396 //! Returns null shape if no any shape is found.
397 Standard_EXPORT TopoDS_Shape GetSHUOInstance (const Handle(XCAFDoc_GraphNode)& theSHUO) const;
399 //! Search for the component shape by labelks path
400 //! and set SHUO structure for founded label structure
401 //! Returns null attribute if no component in any assembly found.
402 Standard_EXPORT Handle(XCAFDoc_GraphNode) SetInstanceSHUO (const TopoDS_Shape& theShape) const;
404 //! Seaching for component shapes that styled by shuo
405 //! Returns empty sequence of shape if no any shape is found.
406 Standard_EXPORT Standard_Boolean GetAllSHUOInstances (const Handle(XCAFDoc_GraphNode)& theSHUO, TopTools_SequenceOfShape& theSHUOShapeSeq) const;
408 //! Searchs the SHUO by labels of components
409 //! from upper_usage componet to next_usage
410 //! Returns null attribute if no SHUO found
411 Standard_EXPORT static Standard_Boolean FindSHUO (const TDF_LabelSequence& Labels, Handle(XCAFDoc_GraphNode)& theSHUOAttr);
413 //! Convert Shape (compound/compsolid/shell/wire) to assembly
414 Standard_EXPORT Standard_Boolean Expand (const TDF_Label& Shape);
416 //! Method to get NamedData attribute assigned to the given shape label.
417 //! @param theLabel [in] the shape Label
418 //! @param theToCreate [in] create and assign attribute if it doesn't exist
419 //! @return Handle to the NamedData attribute or Null if there is none
420 Standard_EXPORT Handle(TDataStd_NamedData) GetNamedProperties (const TDF_Label& theLabel, const Standard_Boolean theToCreate = Standard_False) const;
422 //! Method to get NamedData attribute assigned to a label of the given shape.
423 //! @param theShape [in] input shape
424 //! @param theToCreate [in] create and assign attribute if it doesn't exist
425 //! @return Handle to the NamedData attribute or Null if there is none
426 Standard_EXPORT Handle(TDataStd_NamedData) GetNamedProperties(const TopoDS_Shape& theShape, const Standard_Boolean theToCreate = Standard_False) const;
428 DEFINE_STANDARD_RTTIEXT(XCAFDoc_ShapeTool,TDF_Attribute)
437 //! Checks recursively if the given assembly item is modified. If so, its
438 //! associated compound is updated. Returns true if the assembly item is
439 //! modified, false -- otherwise.
440 Standard_EXPORT Standard_Boolean updateComponent(const TDF_Label& theAssmLabel,
441 TopoDS_Shape& theUpdatedShape) const;
443 //! Adds a new top-level (creates and returns a new label)
444 //! For internal use. Used by public method AddShape.
445 Standard_EXPORT TDF_Label addShape (const TopoDS_Shape& S, const Standard_Boolean makeAssembly = Standard_True);
447 //! Makes a shape on label L to be a reference to shape refL
448 //! with location loc
449 Standard_EXPORT static void MakeReference (const TDF_Label& L, const TDF_Label& refL, const TopLoc_Location& loc);
451 //! Auxiliary method for Expand
452 //! Add declared under expanded theMainShapeL subshapes to new part label thePart
453 //! Recursively iterate all subshapes of shape from thePart, current shape to iterate its subshapes is theShape.
454 Standard_EXPORT void makeSubShape(const TDF_Label& theMainShapeL, const TDF_Label& thePart, const TopoDS_Shape& theShape, const TopLoc_Location& theLoc);
456 XCAFDoc_DataMapOfShapeLabel myShapeLabels;
457 XCAFDoc_DataMapOfShapeLabel mySubShapes;
458 XCAFDoc_DataMapOfShapeLabel mySimpleShapes;
459 Standard_Boolean hasSimpleShapes;
470 #endif // _XCAFDoc_ShapeTool_HeaderFile