# 数据交换

# 1. 快速读取几何数据

很多情况下的应用不关心物体的颜色,此时可以采用StepIO/IgesIO实现快速的模型加载。

# STEP

            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Filter = "STEP (*.stp;*.step)|*.stp;*.step";
            if (dialog.ShowDialog() != DialogResult.OK)
                return;

            var shape = StepIO.Open(dialog.FileName);
            if (shape == null)
                return;

            mRenderView.ShowShape(shape, new Vector3(0.8f)); // 显示指定个灰白色
1
2
3
4
5
6
7
8
9
10

# IGES

            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Filter = "IGES (*.igs;*.iges)|*.igs;*.iges";
            if (dialog.ShowDialog() != DialogResult.OK)
                return;

            var shape = IgesIO.Open(dialog.FileName);
            if (shape == null)
                return;

            // 转换成BREP格式
            BrepIO.Save(shape, dialog.FileName + ".brep");
1
2
3
4
5
6
7
8
9
10
11

TIP

STEP/IGES文件的读取还是很慢。如果需要每次都加载同一个模型文件,可以使用BrepIO::Save成中间格式,加速模型读取。

# 2. 读取模型详细数据

获取模型的颜色、名称等信息相对上述方法效率稍有降低。

# 简单用法

使用StepReader得到每一个TopoShape

            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Filter = "STEP (*.stp;*.step)|*.stp;*.step";
            if (dialog.ShowDialog() != DialogResult.OK)
                return;

            StepReader doc = new StepReader();
            doc.Open(dialog.FileName, (XdeNode xn, TopoShape shape, GTrsf trf, Vector3 color) =>
            {
                // 
                mRenderView.ShowShape(shape.Transformed(trf), color);
            });

1
2
3
4
5
6
7
8
9
10
11
12

# 复杂用法

StepReader是对XdeDocument的封装,若需要更灵活的控制,可以直接使用XdeDocument API来自行解析。


       public class StepReader
        {
            XdeDocument mDocument;
            public Vector3 DefalutColor = new Vector3(0.8f);
            public StepReader()
            {

            }

            public delegate void OnLoadShapeHandler(XdeNode node, TopoShape shape, GTrsf trf, Vector3 color);

            OnLoadShapeHandler mCallback;
            public bool Open(String fileName, OnLoadShapeHandler callback)
            {
                mDocument = new XdeDocument();
                if (!mDocument.Open(fileName))
                    return false;

                mCallback = callback;
                GTrsf trf = new GTrsf();
                // 挨个遍历节点
                for (int idx=0, len=mDocument.GetShapeCount(); idx<len; ++idx)
                {
                    var node = mDocument.GetShapeNode(idx);
                    VisitChildNode(node, trf);
                }

                return true;
            }

            void VisitChildNode(XdeNode node, GTrsf trf)
            {
                // 是否是引用
                if (mDocument.IsReference(node))
                {
                    var shape = mDocument.GetShape(node);
                    var localTrf = shape.GetLocalTransform();
                    VisitChildNode(mDocument.GetReferencedShape(node), trf.Multiplied(localTrf));
                }

                // 是否是个装配体
                else if(mDocument.IsAssembly(node))
                {
                    for (var itr = new XdeNodeIterator(node); itr.More(); itr.Next())
                    {
                        VisitChildNode(itr.Value(), trf);
                    }
                }

                // 是否是个简单的对象
                else if (mDocument.IsSimpleShape(node))
                {
                    var shape = mDocument.GetShape(node);
                    var localTrf = shape.GetLocalTransform();
                    var globalTrf = trf.Multiplied(localTrf);
                    var color = mDocument.GetFaceColor(shape, DefalutColor);

                    //以下是解析到面、边。若不需要则可以直接返回Shape
                    var shapeType = shape.GetShapeType();
                    if (shapeType == EnumTopoShapeType.Topo_FACE)
                    {
                        mCallback(node, shape, globalTrf, color);
                    }
                    else if (shapeType > EnumTopoShapeType.Topo_FACE)
                    {

                        mCallback(node, shape, globalTrf, color);
                    }
                    else
                    {
                        var faces = shape.GetChildren(EnumTopoShapeType.Topo_FACE);
                        foreach(var face in faces)
                        {
                            mCallback(node, face, globalTrf, mDocument.GetFaceColor(face, color));
                        }
                    }
                }                       
            }
        }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80

# 3. 通用模型数据

通用的三维模型格式有obj、stl、3ds、glTF等,这类数据格式只包含了显示相关数据,不包含几何拓扑数据和参数化数据。 ARS提供了SceneIO可以读取此类格式。

            OpenFileDialog dlg = new OpenFileDialog();
            // 获取支持的文件格式列表
            dlg.Filter = SceneIO.FormatFilters();
            if (dlg.ShowDialog() != DialogResult.OK)
                return;

             var node = SceneIO.Load(dlg.FileName, mRenderView.GetMaterialManager());
            if (node == null)
                return;
            mRenderView.ShowSceneNode(node);
            mRenderView.ZoomAll();
1
2
3
4
5
6
7
8
9
10
11
Last Updated: 8/25/2020, 8:57:36 PM