AnyCAD C#开发-StepReader::Read读取STEP

Form1.cs

AnyCAD2020+VS2012

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using AnyCAD.Platform;
using AnyCAD.Exchange;
using AnyCAD.Presentation;

namespace WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        AnyCAD.Presentation.RenderWindow3d renderView;
        AnyCAD.Platform.BrepTools BrepToolsFeat = new AnyCAD.Platform.BrepTools();
        AnyCAD.Exchange.StepReader reader = new AnyCAD.Exchange.StepReader();

        public Form1()
        {
            InitializeComponent();

            this.renderView = new AnyCAD.Presentation.RenderWindow3d();
            this.Controls.Add(renderView);
            renderView.Size = this.ClientSize;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //创建块1
            TopoShape box1 = BrepToolsFeat.MakeBox(new Vector3(0, 0, 0), new Vector3(0, 0, 1), new Vector3(100, 50, 20));

            //把体上所有的边倒角
            TopoShape NewBox1 = BrepToolsFeat.Chamfer(box1, 5, 8);

            //显示几何
            int NewBox1ID = 0;
            renderView.ShowGeometry(NewBox1, NewBox1ID);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //读取STEP
            CADBrower browser = new CADBrower(this.renderView);
            reader.Read("D:\model1.stp", browser);
        }
    }
}

Caesar卢尚宇
2020年6月28日

CADBrower.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AnyCAD.Platform;
using AnyCAD.Presentation;
using System.IO;

namespace WindowsFormsApplication3
{
    class CADBrower : AnyCAD.Platform.TopoShapeReaderContext
    {

        private System.Windows.Forms.TreeView treeView = null;
        private AnyCAD.Presentation.RenderWindow3d renderView = null;
        private Stack<System.Windows.Forms.TreeNode> nodeStack = new Stack<System.Windows.Forms.TreeNode>();
        private int nShapeCount = 100;
        private FaceStyle faceStyle;
        private LineStyle holeStyle;
        private System.Collections.Generic.Dictionary<int, FaceStyle> faceStyleDict = new System.Collections.Generic.Dictionary<int, FaceStyle>();
        public CADBrower(AnyCAD.Presentation.RenderWindow3d _renderView)
        {
            renderView = _renderView;
            faceStyle = new FaceStyle();
            holeStyle = new LineStyle();
            holeStyle.SetLineWidth(3);
            holeStyle.SetColor(0, 256, 0);

        }

        ~CADBrower()
        {
 
        }
        public override void OnSetFaceColor(ColorValue clr)
        {
            if (clr.ToRGBA() == faceStyle.GetColor().ToRGBA())
                return;

            FaceStyle fs = null;
            if (!faceStyleDict.TryGetValue(clr.ToRGBA(), out fs))
            {
                fs = new FaceStyle();
                fs.SetColor(clr);
                faceStyleDict.Add(clr.ToRGBA(), fs);
            }

            faceStyle = fs;
        }
        

        public override void OnBeginGroup(String name)
        {
            if (name.Length == 0)
            {
                name = "<UNKNOWN>";
            }

            if (nodeStack.Count == 0)
            {

            }
            else
            {
                nodeStack.Push(nodeStack.Peek().Nodes.Add(name));
            }
        }

        public override void OnEndGroup()
        {

        }

        public override bool OnBeiginComplexShape(TopoShape shape)
        {
            ++nShapeCount;
            String type = "Shape";
            var st = shape.GetShapeType();
            if (st == EnumTopoShapeType.Topo_COMPOUND)
            {
                type = "Compound";
            }
            else if(st == EnumTopoShapeType.Topo_COMPSOLID)
            {
                type = "CompSolid";
            }
            else if(st == EnumTopoShapeType.Topo_SOLID)
            {
                type = "Solid";
            }
            else if(st == EnumTopoShapeType.Topo_SHELL)
            {
                type = "Shell";
            }
 
            return true;
        }

        public override void OnEndComplexShape()
        {
  
        }

        public override void OnFace(TopoShape face)
        {
            ++nShapeCount;

            SceneNode node = renderView.ShowGeometry(face, nShapeCount);
            node.SetFaceStyle(faceStyle);

            GeomSurface gs = new GeomSurface();
            gs.Initialize(face);
            if (gs.IsUClosed() || gs.IsVClosed())
            {
                WireClassifier wc = new WireClassifier();
                if (!wc.Initialize(face))
                    return;

                var holes = wc.GetInnerWires();
                for (int ii = 0, len = holes.Size(); ii < len; ++ii)
                {
                    var holeEdge = holes.GetAt(ii);
                    ++nShapeCount;
                    var holeNode = renderView.ShowGeometry(holeEdge, nShapeCount);
                    holeNode.SetLineStyle(holeStyle);
                }
            }

        }
    }
}

Caesar卢尚宇
2020年6月28日

原文地址:https://www.cnblogs.com/nxopen2018/p/13499352.html