今天发现AnyCAD.Net SDK发布了,甚是激动,马上同步下代码和SDK使用一下。因为这个东东是这个领域少有的免费的开发包!

按照官方网站上的说法,AnyCAD .Net SDK是给C#的开发者提供的CAD API,而实际上其他.Net的开发者(VB.Net)都可以用。

API for C#

(图片来自www.anycad.net)

 

1 下载

在github上可以下载该SDK。解压后大小不到20M,相比其他CAD的SDK来说,确实小!

 

2 在项目中引用Assembly:

 

3. 我认为一些有用的类

 

4. 代码

(摘自官方的示例程序)

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

using AnyCAD.Platform;

namespace AnyCAD.Basic
{
    public partial class FormMain : Form
    {
        // The global application object
        Platform.Application theApplication = new Platform.Application();
        // BREP tool to create geometries.
        BrepTools shapeMaker = new BrepTools();
        // Default 3d View
        Platform.View3d theView;


        public FormMain()
        {
            InitializeComponent();
            this.MouseWheel += new System.Windows.Forms.MouseEventHandler(this.OnMouseWheel);
        }

        private void FormMain_Load(object sender, EventArgs e)
        {
            // Initialize the Application.
            theApplication.Initialize();
            Size size = panel3d.Size;

            // Create the 3d View
            theView = theApplication.CreateView(panel3d.Handle.ToInt32(), size.Width, size.Height);

            theView.RequestDraw();

            this.timerDraw.Enabled = true;
        }
        private void FormMain_SizeChanged(object sender, EventArgs e)
        {
            Size size = panel3d.Size;
            if(theView != null)
                theView.OnSize(size.Width, size.Height);
        }
        private void panel3d_Paint(object sender, PaintEventArgs e)
        {
            if (theView == null)
                return;

            theView.Redraw();
        }

        private void timerDraw_Tick(object sender, EventArgs e)
        {
            theView.RequestDraw();
            theView.Redraw();
        }

        private SceneNode ShowTopoShape(TopoShape topoShape, int id)
        {
            // Add the TopoShape to Scene.
            TopoShapeConvert convertor = new TopoShapeConvert();
            SceneNode faceNode = convertor.ToFaceNode(topoShape, 0.5f);
            faceNode.SetId(id);
            theView.GetSceneManager().AddNode(faceNode);
            return faceNode;
        }

        private void ClearScene()
        {
            theView.GetSceneManager().ClearNodes();
        }

        private void sphereToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TopoShape sphere = shapeMaker.MakeSphere(new Vector3(0, 0, 0), 40);
            ShowTopoShape(sphere, 100);
        }

        private void boxToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TopoShape box = shapeMaker.MakeBox(new Vector3(40, -20, 0), new Vector3(0, 0, 1), new Vector3(30, 40, 60));
             
            SceneNode sceneNode = ShowTopoShape(box, 101);

            // Face Style
            FaceStyle style = new FaceStyle();
            style.SetColor(new ColorValue(0.5f, 0.3f, 0, 1));
            sceneNode.SetFaceStyle(style);
        }

        private void cylinderToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TopoShape cylinder = shapeMaker.MakeCylinder(new Vector3(80, 0, 0), new Vector3(0, 0, 1), 20, 100, 315);
            SceneNode sceneNode = ShowTopoShape(cylinder, 102);
            FaceStyle style = new FaceStyle();
            style.SetColor(new ColorValue(0.1f, 0.3f, 0.8f, 1));
            sceneNode.SetFaceStyle(style);
        }

        private void coneToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TopoShape cylinder = shapeMaker.MakeCone(new Vector3(120, 0, 0), new Vector3(0, 0, 1), 20, 100, 40, 315);
            ShowTopoShape(cylinder, 103);
        }

        private void extrudeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int size = 20;
            // Create the outline edge
            TopoShape arc = shapeMaker.MakeArc3Pts(new Vector3(-size, 0, 0), new Vector3(size, 0, 0), new Vector3(0, size, 0));
            TopoShape line1 = shapeMaker.MakeLine(new Vector3(-size, -size, 0), new Vector3(-size, 0, 0));
            TopoShape line2 = shapeMaker.MakeLine(new Vector3(size, -size, 0), new Vector3(size, 0, 0));
            TopoShape line3 = shapeMaker.MakeLine(new Vector3(-size, -size, 0), new Vector3(size, -size, 0));

            TopoShapeGroup shapeGroup = new TopoShapeGroup();
            shapeGroup.Add(line1);
            shapeGroup.Add(arc);
            shapeGroup.Add(line2);
            shapeGroup.Add(line3);

            TopoShape wire = shapeMaker.MakeWire(shapeGroup);
            TopoShape face = shapeMaker.MakeFace(wire);

            // Extrude
            TopoShape extrude = shapeMaker.Extrude(face, 100, new Vector3(0, 0, 1));
            ShowTopoShape(extrude, 104);

            // Check find....
            SceneNode findNode = theView.GetSceneManager().FindNode(104);
            theView.GetSceneManager().SelectNode(findNode);
        }

        private void revoleToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int size = 10;
            // Create the outline edge
            TopoShape arc = shapeMaker.MakeArc3Pts(new Vector3(-size, 0, 0), new Vector3(size, 0, 0), new Vector3(0, size, 0));
            TopoShape line1 = shapeMaker.MakeLine(new Vector3(-size, -size, 0), new Vector3(-size, 0, 0));
            TopoShape line2 = shapeMaker.MakeLine(new Vector3(size, -size, 0), new Vector3(size, 0, 0));
            TopoShape line3 = shapeMaker.MakeLine(new Vector3(-size, -size, 0), new Vector3(size, -size, 0));

            TopoShapeGroup shapeGroup = new TopoShapeGroup();
            shapeGroup.Add(line1);
            shapeGroup.Add(arc);
            shapeGroup.Add(line2);
            shapeGroup.Add(line3);

            TopoShape wire = shapeMaker.MakeWire(shapeGroup);

            TopoShape revole = shapeMaker.Revol(wire, new Vector3(size * 3, 0, 0), new Vector3(0, 1, 0), 145);

            ShowTopoShape(revole, 105);
        }

        private void clearToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ClearScene();
        }

        private void sTLToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter = "STL (*.stl)|*.stl|All Files(*.*)|*.*";

            if (DialogResult.OK == dlg.ShowDialog())
            {
                TopoShape shape =  shapeMaker.LoadFile(dlg.FileName);
                if( shape != null)
                    ShowTopoShape(shape, 1000);

            }
        }

        private void Pane3d_MouseDown(object sender, MouseEventArgs e)
        {
            ViewUtility.OnMouseDownEvent(theView, e);
        }

        private void Pane3d_MouseMove(object sender, MouseEventArgs e)
        {
            ViewUtility.OnMouseMoveEvent(theView, e);
        }

        private void Pane3d_MouseUp(object sender, MouseEventArgs e)
        {
            ViewUtility.OnMouseUpEvent(theView, e);
        }
        private void OnMouseWheel(object sender, MouseEventArgs e)
        {
            ViewUtility.OnMouseWheelEvent(theView, e);
        }


    }
}

  

5. 运行效果

 

6. 结论

  • 建模功能满足基本的需求
    • 支持基本体
    • 支持创建拉伸,扫掠,回转体等
  • 支持自定义的显示效果设置
  • 视图操作符合CAD的操作习惯
  • 与AutoCAD相比最大的一个优势就是不是二次开发的方式,直接可以创建独立的应用程序
  • 这个东东是免费的