用C#实现外部调用CAD绘图一例
<p>实现外部程序调用CAD程序,代码中加入了自定义了的类——PF (PublicFunctions),类中包含了几个很实用的函数,如CreateAutoCADObject()可以创建打开一个新的AutoCAD文档,还有绘制线(Line)、文字(Text)和圆(Circel)等函数。注释不多,但仔细研究一下还是对自己很有帮助的。</p> 本帖最后由 作者 于 2009-5-31 23:10:53 编辑 <br /><br /> <p class="code"><code>using System;<br/>using System.Collections.Generic;<br/>using System.Text;<br/>using System.Diagnostics;<br/>using System.Windows.Forms;<br/>using AutoCAD;<br/><br/>namespace CADAutomation<br/>{<br/><br/> public static class PF<br/> {<br/> public static AcadApplication gbl_app;<br/> public static AcadDocument gbl_doc;<br/> public static AcadModelSpaceClass gbl_modSpace;<br/> public static AcadAcCmColor gbl_color;<br/> public static double gbl_pi = 3.14159;<br/> //Layer For Donuts<br/> public static AcadLayer TerminalsLayer;<br/> public static AcadLayer SwitchLayer;<br/> //Layer Termination Points<br/> public static AcadLayer TerminationPoints;<br/><br/> #region PublicFunctions<br/><br/> public static void CloseAllInstance()<br/> {<br/> Process[] aCAD =<br/> Process.GetProcessesByName("acad");<br/><br/> foreach (Process aCADPro in aCAD)<br/> {<br/> aCADPro.CloseMainWindow();<br/> }<br/> }<br/><br/> <br/><br/> public static void CreateAutoCADObject()<br/> {<br/> try<br/> {<br/> CloseAllInstance();<br/> gbl_app = new AcadApplication();<br/> gbl_doc = gbl_app.ActiveDocument;<br/> gbl_app.Application.Visible = true;<br/> gbl_modSpace = (AcadModelSpaceClass)gbl_doc.ModelSpace;<br/> gbl_doc.Linetypes.Load("HIDDEN", "acad.lin");<br/> gbl_doc.Linetypes.Load("CENTER", "acad.lin");<br/><br/> //Other Objects Layer<br/><br/> SwitchLayer = PF.gbl_doc.Layers.Add("Switch110Layer");<br/> SwitchLayer.color = AutoCAD.AcColor.acGreen;<br/> PF.gbl_doc.ActiveLayer = SwitchLayer;<br/><br/> //Layer For Donuts<br/><br/> TerminalsLayer = PF.gbl_doc.Layers.Add("TerminalsLayer");<br/> TerminalsLayer.color = AutoCAD.AcColor.acRed;<br/><br/> //Layer Termination Points<br/><br/> TerminationPoints = <br/> PF.gbl_doc.Layers.Add("TerminationPoints");<br/> TerminationPoints.color = AutoCAD.AcColor.acWhite;<br/><br/> <br/><br/> }<br/> catch (Exception ex)<br/> {<br/> MessageBox.Show(ex.Message);<br/> }<br/> }<br/><br/> public static void DrawLine(double StartX1,<br/> double StartY1,<br/> double EndX2,<br/> double EndY2,<br/> string LineType,<br/> bool DrawDonutsOnLineStart,<br/> bool DrawDonutsOnLineEnds)<br/> {<br/><br/> AcadLine lineObj;<br/> double[] startPoint = new double;<br/> double[] endPoint = new double; ;<br/><br/> startPoint = StartX1;<br/> startPoint = StartY1;<br/> startPoint = 0.0;<br/> endPoint = EndX2;<br/><br/> endPoint = EndY2;<br/> endPoint = 0.01;<br/> lineObj = gbl_doc.ModelSpace.AddLine(startPoint, endPoint);<br/><br/> if (LineType.Length > 0)<br/> {<br/> lineObj.Linetype = LineType; //'"HIDDEN"<br/> lineObj.LinetypeScale = 10;<br/> lineObj.Update();<br/> }<br/><br/> <br/><br/> if (DrawDonutsOnLineStart == true)<br/> {<br/> DrawDonut((AcadBlock)gbl_doc.ModelSpace, <br/> 0, 3.0, StartX1, StartY1);<br/><br/> }<br/><br/> if (DrawDonutsOnLineEnds == true)<br/> {<br/> DrawDonut((AcadBlock)gbl_doc.ModelSpace, <br/> 0, 3.0, EndX2, EndY2);<br/><br/> }<br/> gbl_app.ZoomAll();<br/> }<br/><br/> public static void DrawLine(double StartX1,<br/> double StartY1,<br/> double EndX2,<br/> double EndY2)<br/> {<br/> DrawLine(StartX1, StartY1, EndX2, EndY2, "", false, false);<br/> }<br/><br/> public static void DrawLine(double StartX1,<br/> double StartY1,<br/> double EndX2,<br/> double EndY2,<br/> string LineType)<br/> {<br/> DrawLine(StartX1, StartY1, EndX2, EndY2, <br/> LineType, false, false);<br/><br/> }<br/><br/> public static void DrawLine(double StartX1,<br/> double StartY1,<br/> double EndX2,<br/> double EndY2,<br/> string LineType,<br/> bool DrawDonutsOnLineStart)<br/><br/> {<br/> DrawLine(StartX1, StartY1, EndX2,EndY2, LineType, DrawDonutsOnLineStart, false);<br/> }<br/><br/> public static AcadLWPolyline DrawDonut(AcadBlock space,double inRad,<br/> double outRad,<br/> double cenPt1,double cenPt2)<br/> {<br/><br/> double width, radius, PI;<br/> double[] tmp = new double;<br/> double[] v = new double;<br/> AcadLWPolyline pl;<br/> double[] basePnt = new double;<br/><br/> try<br/> {<br/> //Switch to terminals layer<br/> gbl_doc.ActiveLayer = TerminalsLayer;<br/><br/> basePnt = cenPt1;<br/> basePnt = cenPt2;<br/> basePnt = 0.0;<br/> PI = Math.Atan(1) * 4;<br/> width = (outRad - inRad) / 2;<br/> radius = (inRad + width) / 2;<br/> tmp = (double[])gbl_doc.Utility.PolarPoint(basePnt, <br/> PI, radius);<br/><br/> v = tmp;<br/> v = tmp;<br/> tmp = (double[])gbl_doc.Utility.PolarPoint(basePnt, <br/> 0, radius);<br/> v = tmp;<br/> v = tmp;<br/> pl = space.AddLightWeightPolyline(v); <br/><br/> pl.Closed = true;<br/> pl.SetWidth(0, width, width);<br/> pl.SetBulge(0, -1);<br/> pl.SetWidth(1, width, width);<br/> pl.SetBulge(1, -1);<br/><br/> //Switch to other layer<br/> gbl_doc.ActiveLayer = SwitchLayer; <br/><br/> return pl;<br/> }<br/><br/> catch (Exception ex)<br/> {<br/> MessageBox.Show(ex.Message);<br/> return null;<br/><br/> }<br/> } <br/><br/> public static void DrawSolid(double StartingXPoint,<br/> double StartingYPoint,<br/> double Length,<br/> double Width)<br/> {<br/><br/> AcadSolid solidObj;<br/> double[] point1 = new double;<br/> double[] point2 = new double;<br/> double[] point3 = new double;<br/> double[] point4 = new double;<br/><br/> //Solid Starts<br/><br/> point1 = StartingXPoint;<br/> point1 = StartingYPoint;<br/> point1 = 0.0;<br/> point2 = StartingXPoint;<br/> point2 = (StartingYPoint) - Width;<br/> point2 = 0.0;<br/><br/> point3 = StartingXPoint + Length;<br/> point3 = StartingYPoint;<br/> point3 = 0.0;<br/> point4 = StartingXPoint + Length;<br/> point4 = (StartingYPoint) - Width;<br/> point4 = 0.0;<br/> solidObj = gbl_doc.ModelSpace.AddSolid(point1, <br/> point2, point3, point4);<br/><br/> //Solid ENDS<br/> } <br/><br/> public static void DrawText(double StartingXPoint,<br/> double StartingYPoint,<br/> string textString,<br/> double Height,<br/> double Rotation)<br/> {<br/> AcadText textObj;<br/> double[] insertionPoint = new double;<br/><br/> insertionPoint = StartingXPoint;<br/> insertionPoint = StartingYPoint;<br/> insertionPoint = 0.0;<br/> textObj = gbl_doc.ModelSpace.AddText(textString, <br/> insertionPoint, Height);<br/> textObj.Alignment = AcAlignment.acAlignmentLeft;<br/> textObj.Backward = false;<br/> textObj.Rotation = Rotation;<br/> }<br/><br/> public static void DrawText(double StartingXPoint,double StartingYPoint,<br/> string textString)<br/> {<br/> DrawText(StartingXPoint, StartingYPoint, textString, 3, 0);<br/> }<br/><br/> <br/><br/> public static void AddText(double StartingXPoint,<br/> double StartingYPoint,<br/> string textString,<br/> double Height)<br/> {<br/> DrawText(StartingXPoint, StartingYPoint, textString, Height, 0);<br/> }<br/><br/> <br/><br/> <br/><br/> public static void DrawCircle(double StartingXPoint,<br/> double StartingYPoint,<br/> double Radius)<br/><br/> {<br/> AcadCircle circleObj;<br/> double[] centerPoint = new double;<br/><br/> centerPoint = StartingXPoint;<br/> centerPoint = StartingYPoint;<br/> centerPoint = 0.0;<br/> circleObj = gbl_doc.ModelSpace.AddCircle(centerPoint, Radius);<br/> } <br/><br/> public static void DrawArc(double StartingXPoint,<br/> double StartingYPoint,<br/> double Radius)<br/><br/> {<br/> //For Drawing Arc<br/> AcadArc arcObj;<br/> AcadCircle circleObj;<br/> double[] centerPoint = new double;<br/> double startAngleInDegree;<br/> double endAngleInDegree;<br/> double startAngleInRadian;<br/> double endAngleInRadian; <br/><br/> //Draw Arc<br/> centerPoint = StartingXPoint;<br/> centerPoint = StartingYPoint;<br/> startAngleInDegree = 175.0;<br/> endAngleInDegree = 5.0;<br/> startAngleInRadian = startAngleInDegree * 3.141592 / 180.0;<br/> endAngleInRadian = endAngleInDegree * 3.141592 / 180.0;<br/> arcObj = gbl_doc.ModelSpace.AddArc(centerPoint, Radius,<br/> startAngleInRadian, endAngleInRadian);<br/><br/> } <br/><br/> public static void DrawTerminationPoint(double StartingXPoint,<br/> double StartingYPoint)<br/> {<br/> gbl_doc.ActiveLayer = TerminationPoints;<br/> DrawCircle(StartingXPoint, StartingYPoint, 1.8);<br/> DrawLine(StartingXPoint - 2.5, StartingYPoint - 4.0,<br/> (StartingXPoint - 2.5) + 4.0, <br/> (StartingYPoint - 4.0) + 7.0);<br/><br/> gbl_doc.ActiveLayer = SwitchLayer;<br/> }<br/> #endregion<br/> }<br/>}</code></p> Source_Liu 发表于 2009-5-30 22:40 static/image/common/back.gif本帖最后由 作者 于 2009-5-31 23:10:53 编辑using System;using System.Collections.Generic ...
此处如何修改文字属性?可否提供属性修改范例,谢谢 不错。谢谢!
页:
[1]