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_LabelMap.hxx>
27 #include <TDF_LabelSequence.hxx>
28 #include <Standard_Integer.hxx>
29 #include <Standard_OStream.hxx>
30 #include <TColStd_SequenceOfHAsciiString.hxx>
31 #include <TDF_AttributeSequence.hxx>
32 #include <TopTools_SequenceOfShape.hxx>
36 class TopLoc_Location;
38 class TDF_RelocationTable;
39 class XCAFDoc_GraphNode;
42 class XCAFDoc_ShapeTool;
43 DEFINE_STANDARD_HANDLE(XCAFDoc_ShapeTool, TDF_Attribute)
45 //! A tool to store shapes in an XDE
46 //! document in the form of assembly structure, and to maintain this structure.
47 //! Attribute containing Shapes section of DECAF document.
48 //! Provide tools for management of Shapes section.
49 //! The API provided by this class allows to work with this
50 //! structure regardless of its low-level implementation.
51 //! All the shapes are stored on child labels of a main label which is
52 //! XCAFDoc_DocumentTool::LabelShapes(). The label for assembly also has
53 //! sub-labels, each of which represents the instance of
54 //! another shape in that assembly (component). Such sub-label
55 //! stores reference to the label of the original shape in the form
56 //! of TDataStd_TreeNode with GUID XCAFDoc::ShapeRefGUID(), and its
57 //! location encapsulated into the NamedShape.
58 //! For correct work with an XDE document, it is necessary to use
59 //! methods for analysis and methods for working with shapes.
61 //! if ( STool->IsAssembly(aLabel) )
62 //! { Standard_Boolean subchilds = Standard_False; (default)
63 //! Standard_Integer nbc = STool->NbComponents
64 //! (aLabel[,subchilds]);
66 //! If subchilds is True, commands also consider sub-levels. By
67 //! default, only level one is checked.
68 //! In this example, number of children from the first level of
69 //! assembly will be returned. Methods for creation and initialization:
71 //! XCAFDoc_ShapeTool::XCAFDoc_ShapeTool()
73 //! Standard_GUID GetID ();
74 //! Creation (if does not exist) of ShapeTool on label L:
75 //! Handle(XCAFDoc_ShapeTool) XCAFDoc_ShapeTool::Set(const TDF_Label& L)
76 //! Analyze whether shape is a simple shape or an instance or a
77 //! component of an assembly or it is an assembly ( methods of analysis).
79 //! STool->IsShape(aLabel) ;
80 //! Analyze that the label represents a shape (simple
81 //! shape, assembly or reference) or
82 //! STool->IsTopLevel(aLabel);
83 //! Analyze that the label is a label of a top-level shape.
84 //! Work with simple shapes, assemblies and instances (
85 //! methods for work with shapes).
88 //! Standard_Boolean makeAssembly;
89 //! // True to interpret a Compound as an Assembly, False to take it
91 //! aLabel = STool->AddShape(aShape, makeAssembly);
93 //! TDF_Label aLabel...
94 //! // A label must be present if
95 //! (aLabel.IsNull()) { ... no such label : abandon .. }
96 //! TopoDS_Shape aShape;
97 //! aShape = STool->GetShape(aLabel);
98 //! if (aShape.IsNull())
99 //! { ... this label is not for a Shape ... }
100 //! To get a label from shape.
101 //! Standard_Boolean findInstance = Standard_False;
102 //! (this is default value)
103 //! aLabel = STool->FindShape(aShape [,findInstance]);
104 //! if (aLabel.IsNull())
105 //! { ... no label found for this shape ... }
106 class XCAFDoc_ShapeTool : public TDF_Attribute
112 Standard_EXPORT static const Standard_GUID& GetID();
114 //! Create (if not exist) ShapeTool from XCAFDoc on <L>.
115 Standard_EXPORT static Handle(XCAFDoc_ShapeTool) Set (const TDF_Label& L);
117 //! Creates an empty tool
118 //! Creates a tool to work with a document <Doc>
119 //! Attaches to label XCAFDoc::LabelShapes()
120 Standard_EXPORT XCAFDoc_ShapeTool();
122 //! Returns True if the label is a label of top-level shape,
123 //! as opposed to component of assembly or subshape
124 Standard_EXPORT Standard_Boolean IsTopLevel (const TDF_Label& L) const;
126 //! Returns True if the label is not used by any assembly, i.e.
127 //! contains sublabels which are assembly components
128 //! This is relevant only if IsShape() is True
129 //! (There is no Father TreeNode on this <L>)
130 Standard_EXPORT static Standard_Boolean IsFree (const TDF_Label& L);
132 //! Returns True if the label represents a shape (simple shape,
133 //! assembly or reference)
134 Standard_EXPORT static Standard_Boolean IsShape (const TDF_Label& L);
136 //! Returns True if the label is a label of simple shape
137 Standard_EXPORT static Standard_Boolean IsSimpleShape (const TDF_Label& L);
139 //! Return true if <L> is a located instance of other shape
141 Standard_EXPORT static Standard_Boolean IsReference (const TDF_Label& L);
143 //! Returns True if the label is a label of assembly, i.e.
144 //! contains sublabels which are assembly components
145 //! This is relevant only if IsShape() is True
146 Standard_EXPORT static Standard_Boolean IsAssembly (const TDF_Label& L);
148 //! Return true if <L> is reference serving as component
150 Standard_EXPORT static Standard_Boolean IsComponent (const TDF_Label& L);
152 //! Returns True if the label is a label of compound, i.e.
153 //! contains some sublabels
154 //! This is relevant only if IsShape() is True
155 Standard_EXPORT static Standard_Boolean IsCompound (const TDF_Label& L);
157 //! Return true if <L> is subshape of the top-level shape
158 Standard_EXPORT static Standard_Boolean IsSubShape (const TDF_Label& L);
160 //! Checks whether shape <sub> is subshape of shape stored on
162 Standard_EXPORT Standard_Boolean IsSubShape (const TDF_Label& shapeL, const TopoDS_Shape& sub) const;
164 Standard_EXPORT Standard_Boolean SearchUsingMap (const TopoDS_Shape& S, TDF_Label& L, const Standard_Boolean findWithoutLoc, const Standard_Boolean findSubshape) const;
166 //! General tool to find a (sub) shape in the document
167 //! * If findInstance is True, and S has a non-null location,
168 //! first tries to find the shape among the top-level shapes
169 //! with this location
170 //! * If not found, and findComponent is True, tries to find the shape
171 //! among the components of assemblies
172 //! * If not found, tries to find the shape without location
173 //! among top-level shapes
174 //! * If not found and findSubshape is True, tries to find a
175 //! shape as a subshape of top-level simple shapes
176 //! Returns False if nothing is found
177 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;
179 //! Returns the label corresponding to shape S
180 //! (searches among top-level shapes, not including subcomponents
181 //! of assemblies and subshapes)
182 //! If findInstance is False (default), seach for the
183 //! input shape without location
184 //! If findInstance is True, searches for the
185 //! input shape as is.
186 //! Return True if <S> is found.
187 Standard_EXPORT Standard_Boolean FindShape (const TopoDS_Shape& S, TDF_Label& L, const Standard_Boolean findInstance = Standard_False) const;
189 //! Does the same as previous method
190 //! Returns Null label if not found
191 Standard_EXPORT TDF_Label FindShape (const TopoDS_Shape& S, const Standard_Boolean findInstance = Standard_False) const;
193 //! To get TopoDS_Shape from shape's label
194 //! For component, returns new shape with correct location
195 //! Returns False if label does not contain shape
196 Standard_EXPORT static Standard_Boolean GetShape (const TDF_Label& L, TopoDS_Shape& S);
198 //! To get TopoDS_Shape from shape's label
199 //! For component, returns new shape with correct location
200 //! Returns Null shape if label does not contain shape
201 Standard_EXPORT static TopoDS_Shape GetShape (const TDF_Label& L);
203 //! Creates new (empty) top-level shape.
204 //! Initially it holds empty TopoDS_Compound
205 Standard_EXPORT TDF_Label NewShape() const;
207 //! Sets representation (TopoDS_Shape) for top-level shape.
208 Standard_EXPORT void SetShape (const TDF_Label& L, const TopoDS_Shape& S);
210 //! Adds a new top-level (creates and returns a new label)
211 //! If makeAssembly is True, treats TopAbs_COMPOUND shapes
212 //! as assemblies (creates assembly structure).
213 //! NOTE: <makePrepare> replace components without location
214 //! in assmebly by located components to avoid some problems.
215 //! If AutoNaming() is True then automatically attaches names.
216 Standard_EXPORT TDF_Label AddShape (const TopoDS_Shape& S, const Standard_Boolean makeAssembly = Standard_True, const Standard_Boolean makePrepare = Standard_True);
218 //! Removes shape (whole label and all its sublabels)
219 //! If removeCompletely is true, removes complete shape
220 //! If removeCompletely is false, removes instance(location) only
221 //! Returns False (and does nothing) if shape is not free
222 //! or is not top-level shape
223 Standard_EXPORT Standard_Boolean RemoveShape (const TDF_Label& L, const Standard_Boolean removeCompletely = Standard_True) const;
225 //! set hasComponents into false
226 Standard_EXPORT void Init();
228 //! Sets auto-naming mode to <V>. If True then for added
229 //! shapes, links, assemblies and SHUO's, the TDataStd_Name attribute
230 //! is automatically added. For shapes it contains a shape type
231 //! (e.g. "SOLID", "SHELL", etc); for links it has a form
232 //! "=>[0:1:1:2]" (where a tag is a label containing a shape
233 //! without a location); for assemblies it is "ASSEMBLY", and
234 //! "SHUO" for SHUO's.
235 //! This setting is global; it cannot be made a member function
236 //! as it is used by static methods as well.
237 //! By default, auto-naming is enabled.
238 //! See also AutoNaming().
239 Standard_EXPORT static void SetAutoNaming (const Standard_Boolean V);
241 //! Returns current auto-naming mode. See SetAutoNaming() for
243 Standard_EXPORT static Standard_Boolean AutoNaming();
246 Standard_EXPORT void ComputeShapes (const TDF_Label& L);
248 //! Compute a sequence of simple shapes
249 Standard_EXPORT void ComputeSimpleShapes();
251 //! Returns a sequence of all top-level shapes
252 Standard_EXPORT void GetShapes (TDF_LabelSequence& Labels) const;
254 //! Returns a sequence of all top-level shapes
255 //! which are free (i.e. not referred by any other)
256 Standard_EXPORT void GetFreeShapes (TDF_LabelSequence& FreeLabels) const;
258 //! Returns list of labels which refer shape L as component
259 //! Returns number of users (0 if shape is free)
260 Standard_EXPORT static Standard_Integer GetUsers (const TDF_Label& L, TDF_LabelSequence& Labels, const Standard_Boolean getsubchilds = Standard_False);
262 //! Returns location of instance
263 Standard_EXPORT static TopLoc_Location GetLocation (const TDF_Label& L);
265 //! Returns label which corresponds to a shape referred by L
266 //! Returns False if label is not reference
267 Standard_EXPORT static Standard_Boolean GetReferredShape (const TDF_Label& L, TDF_Label& Label);
269 //! Returns number of Assembles components
270 Standard_EXPORT static Standard_Integer NbComponents (const TDF_Label& L, const Standard_Boolean getsubchilds = Standard_False);
272 //! Returns list of components of assembly
273 //! Returns False if label is not assembly
274 Standard_EXPORT static Standard_Boolean GetComponents (const TDF_Label& L, TDF_LabelSequence& Labels, const Standard_Boolean getsubchilds = Standard_False);
276 //! Adds a component given by its label and location to the assembly
277 //! Note: assembly must be IsAssembly() or IsSimpleShape()
278 Standard_EXPORT TDF_Label AddComponent (const TDF_Label& assembly, const TDF_Label& comp, const TopLoc_Location& Loc);
280 //! Adds a shape (located) as a component to the assembly
281 //! If necessary, creates an additional top-level shape for
282 //! component and return the Label of component.
283 //! If expand is True and component is Compound, it will
284 //! be created as assembly also
285 //! Note: assembly must be IsAssembly() or IsSimpleShape()
286 Standard_EXPORT TDF_Label AddComponent (const TDF_Label& assembly, const TopoDS_Shape& comp, const Standard_Boolean expand = Standard_False);
288 //! Removes a component from its assembly
289 Standard_EXPORT void RemoveComponent (const TDF_Label& comp) const;
291 //! Top-down update for all assembly compounds stored in the document.
292 Standard_EXPORT void UpdateAssemblies();
294 //! Finds a label for subshape <sub> of shape stored on
296 //! Returns Null label if it is not found
297 Standard_EXPORT Standard_Boolean FindSubShape (const TDF_Label& shapeL, const TopoDS_Shape& sub, TDF_Label& L) const;
299 //! Adds a label for subshape <sub> of shape stored on
301 //! Returns Null label if it is not subshape
302 Standard_EXPORT TDF_Label AddSubShape (const TDF_Label& shapeL, const TopoDS_Shape& sub) const;
304 //! Adds (of finds already existed) a label for subshape <sub> of shape stored on
305 //! label shapeL. Label addedSubShapeL returns added (found) label or empty in case of wrong subshape.
306 //! Returns True, if new shape was added, False in case of already existed subshape/wrong subshape
307 Standard_EXPORT Standard_Boolean AddSubShape(const TDF_Label& shapeL, const TopoDS_Shape& sub, TDF_Label& addedSubShapeL) const;
309 Standard_EXPORT TDF_Label FindMainShapeUsingMap (const TopoDS_Shape& sub) const;
311 //! Performs a search among top-level shapes to find
312 //! the shape containing <sub> as subshape
313 //! Checks only simple shapes, and returns the first found
314 //! label (which should be the only one for valid model)
315 Standard_EXPORT TDF_Label FindMainShape (const TopoDS_Shape& sub) const;
317 //! Returns list of labels identifying subshapes of the given shape
318 //! Returns False if no subshapes are placed on that label
319 Standard_EXPORT static Standard_Boolean GetSubShapes (const TDF_Label& L, TDF_LabelSequence& Labels);
321 //! returns the label under which shapes are stored
322 Standard_EXPORT TDF_Label BaseLabel() const;
324 Standard_EXPORT Standard_OStream& Dump (Standard_OStream& theDumpLog, const Standard_Boolean deep) const;
326 Standard_EXPORT virtual Standard_OStream& Dump (Standard_OStream& theDumpLog) const Standard_OVERRIDE;
328 //! Print to std::ostream <theDumpLog> type of shape found on <L> label
329 //! and the entry of <L>, with <level> tabs before.
330 //! If <deep>, print also TShape and Location addresses
331 Standard_EXPORT static void DumpShape (Standard_OStream& theDumpLog, const TDF_Label& L, const Standard_Integer level = 0, const Standard_Boolean deep = Standard_False);
333 Standard_EXPORT const Standard_GUID& ID() const Standard_OVERRIDE;
335 Standard_EXPORT void Restore (const Handle(TDF_Attribute)& with) Standard_OVERRIDE;
337 Standard_EXPORT Handle(TDF_Attribute) NewEmpty() const Standard_OVERRIDE;
339 Standard_EXPORT void Paste (const Handle(TDF_Attribute)& into, const Handle(TDF_RelocationTable)& RT) const Standard_OVERRIDE;
341 //! Returns True if the label is a label of external references, i.e.
342 //! there are some reference on the no-step files, which are
343 //! described in document only their names
344 Standard_EXPORT static Standard_Boolean IsExternRef (const TDF_Label& L);
346 //! Sets the names of references on the no-step files
347 Standard_EXPORT TDF_Label SetExternRefs (const TColStd_SequenceOfHAsciiString& SHAS) const;
349 //! Sets the names of references on the no-step files
350 Standard_EXPORT void SetExternRefs (const TDF_Label& L, const TColStd_SequenceOfHAsciiString& SHAS) const;
352 //! Gets the names of references on the no-step files
353 Standard_EXPORT static void GetExternRefs (const TDF_Label& L, TColStd_SequenceOfHAsciiString& SHAS);
355 //! Sets the SHUO structure between upper_usage and next_usage
356 //! create multy-level (if number of labels > 2) SHUO from first to last
357 //! Initialise out <MainSHUOAttr> by main upper_usage SHUO attribute.
358 //! Returns FALSE if some of labels in not component label
359 Standard_EXPORT Standard_Boolean SetSHUO (const TDF_LabelSequence& Labels, Handle(XCAFDoc_GraphNode)& MainSHUOAttr) const;
361 //! Returns founded SHUO GraphNode attribute <aSHUOAttr>
362 //! Returns false in other case
363 Standard_EXPORT static Standard_Boolean GetSHUO (const TDF_Label& SHUOLabel, Handle(XCAFDoc_GraphNode)& aSHUOAttr);
365 //! Returns founded SHUO GraphNodes of indicated component
366 //! Returns false in other case
367 Standard_EXPORT static Standard_Boolean GetAllComponentSHUO (const TDF_Label& CompLabel, TDF_AttributeSequence& SHUOAttrs);
369 //! Returns the sequence of labels of SHUO attributes,
370 //! which is upper_usage for this next_usage SHUO attribute
371 //! (that indicated by label)
372 //! NOTE: returns upper_usages only on one level (not recurse)
373 //! NOTE: do not clear the sequence before filling
374 Standard_EXPORT static Standard_Boolean GetSHUOUpperUsage (const TDF_Label& NextUsageL, TDF_LabelSequence& Labels);
376 //! Returns the sequence of labels of SHUO attributes,
377 //! which is next_usage for this upper_usage SHUO attribute
378 //! (that indicated by label)
379 //! NOTE: returns next_usages only on one level (not recurse)
380 //! NOTE: do not clear the sequence before filling
381 Standard_EXPORT static Standard_Boolean GetSHUONextUsage (const TDF_Label& UpperUsageL, TDF_LabelSequence& Labels);
383 //! Remove SHUO from component sublabel,
384 //! remove all dependencies on other SHUO.
385 //! Returns FALSE if cannot remove SHUO dependencies.
386 //! NOTE: remove any styles that associated with this SHUO.
387 Standard_EXPORT Standard_Boolean RemoveSHUO (const TDF_Label& SHUOLabel) const;
389 //! Serach the path of labels in the document,
390 //! that corresponds the component from any assembly
391 //! Try to search the sequence of labels with location that
392 //! produce this shape as component of any assembly
393 //! NOTE: Clear sequence of labels before filling
394 Standard_EXPORT Standard_Boolean FindComponent (const TopoDS_Shape& theShape, TDF_LabelSequence& Labels) const;
396 //! Search for the component shape that styled by shuo
397 //! Returns null shape if no any shape is found.
398 Standard_EXPORT TopoDS_Shape GetSHUOInstance (const Handle(XCAFDoc_GraphNode)& theSHUO) const;
400 //! Search for the component shape by labelks path
401 //! and set SHUO structure for founded label structure
402 //! Returns null attribute if no component in any assembly found.
403 Standard_EXPORT Handle(XCAFDoc_GraphNode) SetInstanceSHUO (const TopoDS_Shape& theShape) const;
405 //! Seaching for component shapes that styled by shuo
406 //! Returns empty sequence of shape if no any shape is found.
407 Standard_EXPORT Standard_Boolean GetAllSHUOInstances (const Handle(XCAFDoc_GraphNode)& theSHUO, TopTools_SequenceOfShape& theSHUOShapeSeq) const;
409 //! Searchs the SHUO by labels of components
410 //! from upper_usage componet to next_usage
411 //! Returns null attribute if no SHUO found
412 Standard_EXPORT static Standard_Boolean FindSHUO (const TDF_LabelSequence& Labels, Handle(XCAFDoc_GraphNode)& theSHUOAttr);
414 //! Convert Shape (compound/compsolid/shell/wire) to assembly
415 Standard_EXPORT Standard_Boolean Expand (const TDF_Label& Shape);
417 //! Method to get NamedData attribute assigned to the given shape label.
418 //! @param theLabel [in] the shape Label
419 //! @param theToCreate [in] create and assign attribute if it doesn't exist
420 //! @return Handle to the NamedData attribute or Null if there is none
421 Standard_EXPORT Handle(TDataStd_NamedData) GetNamedProperties (const TDF_Label& theLabel, const Standard_Boolean theToCreate = Standard_False) const;
423 //! Method to get NamedData attribute assigned to a label of the given shape.
424 //! @param theShape [in] input shape
425 //! @param theToCreate [in] create and assign attribute if it doesn't exist
426 //! @return Handle to the NamedData attribute or Null if there is none
427 Standard_EXPORT Handle(TDataStd_NamedData) GetNamedProperties(const TopoDS_Shape& theShape, const Standard_Boolean theToCreate = Standard_False) const;
429 //! Dumps the content of me into the stream
430 Standard_EXPORT virtual void DumpJson (Standard_OStream& theOStream, Standard_Integer theDepth = -1) const Standard_OVERRIDE;
432 DEFINE_STANDARD_RTTIEXT(XCAFDoc_ShapeTool,TDF_Attribute)
441 //! Checks recursively if the given assembly item is modified. If so, its
442 //! associated compound is updated. Returns true if the assembly item is
443 //! modified, false -- otherwise.
444 Standard_EXPORT Standard_Boolean updateComponent(const TDF_Label& theAssmLabel,
445 TopoDS_Shape& theUpdatedShape,
446 TDF_LabelMap& theUpdated) const;
448 //! Adds a new top-level (creates and returns a new label)
449 //! For internal use. Used by public method AddShape.
450 Standard_EXPORT TDF_Label addShape (const TopoDS_Shape& S, const Standard_Boolean makeAssembly = Standard_True);
452 //! Makes a shape on label L to be a reference to shape refL
453 //! with location loc
454 Standard_EXPORT static void MakeReference (const TDF_Label& L, const TDF_Label& refL, const TopLoc_Location& loc);
456 //! Auxiliary method for Expand
457 //! Add declared under expanded theMainShapeL subshapes to new part label thePart
458 //! Recursively iterate all subshapes of shape from thePart, current shape to iterate its subshapes is theShape.
459 Standard_EXPORT void makeSubShape(const TDF_Label& theMainShapeL, const TDF_Label& thePart, const TopoDS_Shape& theShape, const TopLoc_Location& theLoc);
461 XCAFDoc_DataMapOfShapeLabel myShapeLabels;
462 XCAFDoc_DataMapOfShapeLabel mySubShapes;
463 XCAFDoc_DataMapOfShapeLabel mySimpleShapes;
464 Standard_Boolean hasSimpleShapes;
475 #endif // _XCAFDoc_ShapeTool_HeaderFile