明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 3148|回复: 3

[运行时] 用C#实现外部调用CAD绘图一例

[复制链接]
发表于 2009-5-30 22:39:00 | 显示全部楼层 |阅读模式

实现外部程序调用CAD程序,代码中加入了自定义了的类——PF (PublicFunctions),类中包含了几个很实用的函数,如CreateAutoCADObject()可以创建打开一个新的AutoCAD文档,还有绘制线(Line)、文字(Text)和圆(Circel)等函数。注释不多,但仔细研究一下还是对自己很有帮助的。

评分

参与人数 1明经币 +5 金钱 +10 贡献 +10 激情 +10 收起 理由
雪山飞狐_lzh + 5 + 10 + 10 + 10 【好评】表扬一下

查看全部评分

 楼主| 发表于 2009-5-30 22:40:00 | 显示全部楼层
本帖最后由 作者 于 2009-5-31 23:10:53 编辑

using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.Windows.Forms;
using AutoCAD;

namespace CADAutomation
{

    public static class F
    {
        public static AcadApplication gbl_app;
        public static AcadDocument gbl_doc;
        public static AcadModelSpaceClass gbl_modSpace;
        public static AcadAcCmColor gbl_color;
        public static double gbl_pi = 3.14159;
        //Layer For Donuts
        public static AcadLayer TerminalsLayer;
        public static AcadLayer SwitchLayer;
        //Layer Termination oints
        public static AcadLayer TerminationPoints;

        #region ublicFunctions

        public static void CloseAllInstance()
        {
            rocess[] aCAD =
               rocess.GetProcessesByName("acad");

            foreach (Process aCADPro in aCAD)
            {
                aCADPro.CloseMainWindow();
            }
        }

 

        public static void CreateAutoCADObject()
        {
            try
            {
                CloseAllInstance();
                gbl_app = new AcadApplication();
                gbl_doc = gbl_app.ActiveDocument;
                gbl_app.Application.Visible = true;
                gbl_modSpace = (AcadModelSpaceClass)gbl_doc.ModelSpace;
                gbl_doc.Linetypes.Load("HIDDEN", "acad.lin");
                gbl_doc.Linetypes.Load("CENTER", "acad.lin");

                //Other Objects Layer

                SwitchLayer = F.gbl_doc.Layers.Add("Switch110Layer");
                SwitchLayer.color = AutoCAD.AcColor.acGreen;
                F.gbl_doc.ActiveLayer = SwitchLayer;

                //Layer For Donuts

                TerminalsLayer = F.gbl_doc.Layers.Add("TerminalsLayer");
                TerminalsLayer.color = AutoCAD.AcColor.acRed;

                //Layer Termination oints

                TerminationPoints = 
                  F.gbl_doc.Layers.Add("TerminationPoints");
                TerminationPoints.color = AutoCAD.AcColor.acWhite;

 

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        public static void DrawLine(double StartX1,
                                    double StartY1,
                                    double EndX2,
                                    double EndY2,
                                    string LineType,
                                    bool DrawDonutsOnLineStart,
                                    bool DrawDonutsOnLineEnds)
        {

            AcadLine lineObj;
            double[] startPoint = new double[3];
            double[] endPoint = new double[3]; ;

            startPoint[0] = StartX1;
            startPoint[1] = StartY1;
            startPoint[2] = 0.0;
            endPoint[0] = EndX2;

            endPoint[1] = EndY2;
            endPoint[2] = 0.01;
            lineObj = gbl_doc.ModelSpace.AddLine(startPoint, endPoint);

            if (LineType.Length > 0)
            {
                lineObj.Linetype = LineType; //'"HIDDEN"
                lineObj.LinetypeScale = 10;
                lineObj.Update();
            }

 

            if (DrawDonutsOnLineStart == true)
            {
                DrawDonut((AcadBlock)gbl_doc.ModelSpace, 
                              0, 3.0, StartX1, StartY1);

            }

            if (DrawDonutsOnLineEnds == true)
            {
                DrawDonut((AcadBlock)gbl_doc.ModelSpace, 
                                  0, 3.0, EndX2, EndY2);

            }
            gbl_app.ZoomAll();
        }

        public static void DrawLine(double StartX1,
                                    double StartY1,
                                    double EndX2,
                                    double EndY2)
        {
            DrawLine(StartX1, StartY1, EndX2, EndY2, "", false, false);
        }

        public static void DrawLine(double StartX1,
                                    double StartY1,
                                    double EndX2,
                                    double EndY2,
                                    string LineType)
        {
            DrawLine(StartX1, StartY1, EndX2, EndY2, 
                              LineType, false, false);

        }

        public static void DrawLine(double StartX1,
                                    double StartY1,
                                    double EndX2,
                                    double EndY2,
                                    string LineType,
                                    bool DrawDonutsOnLineStart)

        {
            DrawLine(StartX1, StartY1, EndX2,EndY2, LineType, DrawDonutsOnLineStart, false);
        }

        public static AcadLWPolyline DrawDonut(AcadBlock space,double inRad,
                                                double outRad,
                                                double cenPt1,double cenPt2)
        {

            double width, radius, I;
            double[] tmp = new double[2];
            double[] v = new double[4];
            AcadLWPolyline pl;
            double[] basePnt = new double[3];

            try
            {
                //Switch to terminals layer
                gbl_doc.ActiveLayer = TerminalsLayer;

                basePnt[0] = cenPt1;
                basePnt[1] = cenPt2;
                basePnt[2] = 0.0;
                I = Math.Atan(1) * 4;
                width = (outRad - inRad) / 2;
                radius = (inRad + width) / 2;
                tmp = (double[])gbl_doc.Utility.PolarPoint(basePnt, 
                                                       I, radius);

                v[0] = tmp[0];
                v[1] = tmp[1];
                tmp = (double[])gbl_doc.Utility.PolarPoint(basePnt, 
                                                        0, radius);
                v[2] = tmp[0];
                v[3] = tmp[1];
                pl = space.AddLightWeightPolyline(v); 

                pl.Closed = true;
                pl.SetWidth(0, width, width);
                pl.SetBulge(0, -1);
                pl.SetWidth(1, width, width);
                pl.SetBulge(1, -1);

                //Switch to other layer
                gbl_doc.ActiveLayer = SwitchLayer;               

                return pl;
            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return null;

            }
        } 

        public static void DrawSolid(double StartingXPoint,
                                        double StartingYPoint,
                                        double Length,
                                        double Width)
        {

            AcadSolid solidObj;
            double[] point1 = new double[3];
            double[] point2 = new double[3];
            double[] point3 = new double[3];
            double[] point4 = new double[3];

            //Solid Starts

            point1[0] = StartingXPoint;
            point1[1] = StartingYPoint;
            point1[2] = 0.0;
            point2[0] = StartingXPoint;
            point2[1] = (StartingYPoint) - Width;
            point2[2] = 0.0;

            point3[0] = StartingXPoint + Length;
            point3[1] = StartingYPoint;
            point3[2] = 0.0;
            point4[0] = StartingXPoint + Length;
            point4[1] = (StartingYPoint) - Width;
            point4[2] = 0.0;
            solidObj  = gbl_doc.ModelSpace.AddSolid(point1, 
                                   point2, point3, point4);

            //Solid ENDS
        } 

        public static void DrawText(double StartingXPoint,
                                    double StartingYPoint,
                                    string textString,
                                    double Height,
                                    double Rotation)
        {
            AcadText textObj;
            double[] insertionPoint = new double[3];

            insertionPoint[0] = StartingXPoint;
            insertionPoint[1] = StartingYPoint;
            insertionPoint[2] = 0.0;
            textObj = gbl_doc.ModelSpace.AddText(textString, 
                                    insertionPoint, Height);
            textObj.Alignment = AcAlignment.acAlignmentLeft;
            textObj.Backward = false;
            textObj.Rotation = Rotation;
        }

        public static void DrawText(double StartingXPoint,double StartingYPoint,
                            string textString)
        {
            DrawText(StartingXPoint, StartingYPoint, textString, 3, 0);
        }

 

        public static void AddText(double StartingXPoint,
                                    double StartingYPoint,
                                    string textString,
                                    double Height)
        {
            DrawText(StartingXPoint, StartingYPoint, textString, Height, 0);
        }

 

 

        public static void DrawCircle(double StartingXPoint,
                                    double StartingYPoint,
                                    double Radius)

        {
            AcadCircle circleObj;
            double[] centerPoint = new double[3];

            centerPoint[0] = StartingXPoint;
            centerPoint[1] = StartingYPoint;
            centerPoint[2] = 0.0;
            circleObj = gbl_doc.ModelSpace.AddCircle(centerPoint, Radius);
        } 

        public static void DrawArc(double StartingXPoint,
                                   double StartingYPoint,
                                   double Radius)

        {
            //For Drawing Arc
            AcadArc arcObj;
            AcadCircle circleObj;
            double[] centerPoint = new double[3];
            double startAngleInDegree;
            double endAngleInDegree;
            double startAngleInRadian;
            double endAngleInRadian; 

            //Draw Arc
            centerPoint[0] = StartingXPoint;
            centerPoint[1] = StartingYPoint;
            startAngleInDegree = 175.0;
            endAngleInDegree = 5.0;
            startAngleInRadian = startAngleInDegree * 3.141592 / 180.0;
            endAngleInRadian = endAngleInDegree * 3.141592 / 180.0;
            arcObj = gbl_doc.ModelSpace.AddArc(centerPoint, Radius,
                startAngleInRadian, endAngleInRadian);

        } 

        public static void DrawTerminationPoint(double StartingXPoint,
                                    double StartingYPoint)
        {
            gbl_doc.ActiveLayer = TerminationPoints;
            DrawCircle(StartingXPoint, StartingYPoint, 1.8);
            DrawLine(StartingXPoint - 2.5, StartingYPoint - 4.0,
               (StartingXPoint - 2.5) + 4.0, 
               (StartingYPoint - 4.0) + 7.0);

           gbl_doc.ActiveLayer = SwitchLayer;
        }
        #endregion
    }
}

发表于 2012-11-17 23:34:28 | 显示全部楼层
Source_Liu 发表于 2009-5-30 22:40
本帖最后由 作者 于 2009-5-31 23:10:53 编辑  using System;using System.Collections.Generic ...

此处如何修改文字属性?可否提供属性修改范例,谢谢
发表于 2012-12-28 09:22:53 | 显示全部楼层
不错。谢谢!
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-11-29 08:57 , Processed in 0.209555 second(s), 27 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表