pythonocc基础使用:模型信息获取(装配关系,材料,颜色)XCAF

占坑。。。
https://www.opencascade.com/doc/occt-7.3.0/overview/html/occt_user_guides__xde.html

https://www.opencascade.com/doc/occt-7.1.0/overview/html/occt_user_guides__iges.html

he Extended Data Exchange (XDE) module allows extending the scope of exchange by translating additional data attached to geometric BREP data, thereby improving the interoperability with external software.

Data types such as colors, layers, assembly descriptions and validation properties (i.e. center of gravity, etc.) are supported. These data are stored together with shapes in an XCAF document. It is also possible to add a new types of data taking the existing tools as prototypes.

OCC.XCAFDoc
XCAFDoc_DocumentTool

Definition of general structure of DECAF documentand tools to work with itThe document is composed of sections, each sectionstoring its own kind of data and managing by correspondingtoolSome properties can be attached directly to shapes. These properties are:* Name (the standard definition from OCAF) – class TDataStd_Name* Centroid (for the validation of transfer) – class XCAFDoc_Centroid* Volume (for the validation of transfer) – class XCAFDoc_Volume* Area (for the validation of transfer) – class XCafDoc_AreaManagement of these attributes is realized by OCAF. For gettingthe attributes attached to a label the method classTDF_Label::FindAttribute() should be used.

在这里插入图片描述此张图片里提到了TDF,后面该代码也使用到了TDF的东西。。

from OCC.Core.TCollection import TCollection_ExtendedString

from OCC.Core.TDocStd import TDocStd_Document
from OCC.Core.XCAFDoc import (XCAFDoc_DocumentTool_ShapeTool,
                              XCAFDoc_DocumentTool_ColorTool,
                              XCAFDoc_DocumentTool_LayerTool,
                              XCAFDoc_DocumentTool_MaterialTool)
from OCC.Core.STEPCAFControl import STEPCAFControl_Reader
from OCC.Core.IFSelect import IFSelect_RetDone
from OCC.Core.TDF import TDF_LabelSequence

from OCC.Display.SimpleGui import init_display

filename = 'a380_asm.stp'
_shapes = []

# create an handle to a document
doc = TDocStd_Document(TCollection_ExtendedString("pythonocc-doc"))

# Get root assembly
shape_tool = XCAFDoc_DocumentTool_ShapeTool(doc.Main())
l_colors = XCAFDoc_DocumentTool_ColorTool(doc.Main())
l_layers = XCAFDoc_DocumentTool_LayerTool(doc.Main())
l_materials = XCAFDoc_DocumentTool_MaterialTool(doc.Main())

step_reader = STEPCAFControl_Reader()
step_reader.SetColorMode(True)
step_reader.SetLayerMode(True)
step_reader.SetNameMode(True)
step_reader.SetMatMode(True)

status = step_reader.ReadFile(filename)
if status == IFSelect_RetDone:
    step_reader.Transfer(doc)

labels = TDF_LabelSequence()
color_labels = TDF_LabelSequence()

shape_tool.GetFreeShapes(labels)

print("Number of shapes at root :%i" % labels.Length())
for i in range(labels.Length()):
    sub_shapes_labels = TDF_LabelSequence()
    print("Is Assembly :", shape_tool.IsAssembly(labels.Value(i+1)))
    sub_shapes = shape_tool.GetSubShapes(labels.Value(i+1), sub_shapes_labels)
    print("Number of subshapes in the assemly :%i" % sub_shapes_labels.Length())
l_colors.GetColors(color_labels)

print("Number of colors=%i" % color_labels.Length())
for i in range(color_labels.Length()):
    color = color_labels.Value(i+1)
    print(color.DumpToString())

for i in range(labels.Length()):
    label = labels.Value(i+1)
    a_shape = shape_tool.GetShape(label)
    m = l_layers.GetLayers(a_shape)
    _shapes.append(a_shape)

#
# Display
#
display, start_display, add_menu, add_function_to_menu = init_display()
display.DisplayShape(_shapes, update=True)
start_display()
Is Assembly : True
Number of subshapes in the assemly :0
Number of colors=4
0:1:2:1	NOT imported; NOT modified; NO attribute modified; has 3 attributes.
	XCAFDoc_Color	Trans. 0; Valid;	ID = efd212f0-6dfd-11d4-b9c8-0060b0ee281b
	TDataStd_Name	Trans. 0; Valid;	ID = 2a96b608-ec8b-11d0-bee7-080009dc3333
	TDataStd_TreeNode	Trans. 0; Valid;	ID = efd212e5-6dfd-11d4-b9c8-0060b0ee281b

0:1:2:2	NOT imported; NOT modified; NO attribute modified; has 4 attributes.
	XCAFDoc_Color	Trans. 0; Valid;	ID = efd212f0-6dfd-11d4-b9c8-0060b0ee281b
	TDataStd_Name	Trans. 0; Valid;	ID = 2a96b608-ec8b-11d0-bee7-080009dc3333
	TDataStd_TreeNode	Trans. 0; Valid;	ID = efd212e6-6dfd-11d4-b9c8-0060b0ee281b
	TDataStd_TreeNode	Trans. 0; Valid;	ID = efd212e5-6dfd-11d4-b9c8-0060b0ee281b

0:1:2:3	NOT imported; NOT modified; NO attribute modified; has 3 attributes.
	XCAFDoc_Color	Trans. 0; Valid;	ID = efd212f0-6dfd-11d4-b9c8-0060b0ee281b
	TDataStd_Name	Trans. 0; Valid;	ID = 2a96b608-ec8b-11d0-bee7-080009dc3333
	TDataStd_TreeNode	Trans. 0; Valid;	ID = efd212e5-6dfd-11d4-b9c8-0060b0ee281b

0:1:2:4	NOT imported; NOT modified; NO attribute modified; has 3 attributes.
	XCAFDoc_Color	Trans. 0; Valid;	ID = efd212f0-6dfd-11d4-b9c8-0060b0ee281b
	TDataStd_Name	Trans. 0; Valid;	ID = 2a96b608-ec8b-11d0-bee7-080009dc3333
	TDataStd_TreeNode	Trans. 0; Valid;	ID = efd212e5-6dfd-11d4-b9c8-0060b0ee281b
``

© 版权声明
THE END
喜欢就支持一下吧
点赞955 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片

    暂无评论内容