C# AutoCAD ActiveX 二次开发 -- CAD图层合并
因使用AutoCAD版本为2004,2006版本以后才有.net开发包,因此采用ActiveX方式进行开发。
功能为:将几个CAD图层合并到其中一个上,并设置坐标,线和标记的颜色和字体。
CAD对象的层级为
AcadApplication -
AcadDocument -
AcadModelSpace
AcadEntity
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Text; 7 using System.Windows.Forms; 8 9 using AutoCAD; 10 11 namespace road 12 { 13 public class CadMerge 14 { 15 #region members 16 17 private AcadApplication app = null; 18 private AcadDocument docLine = null; 19 private AcadDocument docLineAnno = null; 20 private AcadDocument docPoint = null; 21 private AcadDocument docPointAnno = null; 22 23 private AcadModelSpace msLineAnno = null; 24 25 private string dirPath = ""; 26 private string tempPath = ""; 27 private string linePath = ""; 28 private string pointPath = ""; 29 private string pointAnnoPath = ""; 30 private string lineAnnoPath = ""; 31 32 private ProgressBar m_bar = null; 33 34 #endregion 35 36 public CadMerge(string path, ProgressBar bar) 37 { 38 this.dirPath = path; 39 m_bar = bar; 40 init(); 41 } 42 43 //初始化参数 44 private void init() 45 { 46 dirPath = dirPath+"\\"; 47 tempPath = dirPath + "temp\\"; 48 linePath = tempPath + "line.dwg"; 49 lineAnnoPath = dirPath + MainForm.m_resultDwgName; 50 pointPath = tempPath + "point.dwg"; 51 pointAnnoPath = tempPath + "point_anno.dwg"; 52 53 app = new AcadApplication(); 54 m_bar.PerformStep(); 55 //app.Application.Visible = true; 56 docLine = app.Documents.Open(linePath, null, null); 57 docLineAnno = app.Documents.Open(lineAnnoPath, null, null); 58 m_bar.PerformStep(); 59 docPoint = app.Documents.Open(pointPath, null, null); 60 docPointAnno = app.Documents.Open(pointAnnoPath, null, null); 61 m_bar.PerformStep(); 62 } 63 64 //关闭cad文档 65 private void CloseDocuments(AcadApplication app) 66 { 67 AcadDocuments docs = app.Documents; 68 foreach (AcadDocument doc in docs) 69 { 70 if (doc.ReadOnly) 71 { 72 doc.Close(false, null); 73 } 74 else 75 { 76 doc.Close(true, null); 77 } 78 } 79 } 80 81 //合并cad文档 82 public void MergeCADLayers() 83 { 84 msLineAnno = docLineAnno.ModelSpace; 85 AcadModelSpace msLine = docLine.ModelSpace; 86 AcadModelSpace msPoint = docPoint.ModelSpace; 87 AcadModelSpace msPointAnno = docPointAnno.ModelSpace; 88 updateLineAnno(); 89 AddLineToLineAnno(msLineAnno, msLine); 90 m_bar.PerformStep(); 91 AddPointToLineAnno(msLineAnno, msPoint); 92 m_bar.PerformStep(); 93 AddPointAnnoToLineAnno(msLineAnno, msPointAnno); 94 updateColor(); 95 app.Update(); 96 docLineAnno.Save(); 97 CloseDocuments(app); 98 } 99 100 //更新线标记图层101 private void updateLineAnno()102 {103 foreach (AcadEntity entity in msLineAnno)104 {105 if (entity is AcadText)106 {107 AcadText text = (AcadText)entity;108 msLineAnno.AddText(text.TextString, getPoint(text.InsertionPoint), text.Height);109 text.Delete();110 }111 }112 }113 114 //点的转换,新坐标要求115 private double[] getPoint(object from)116 {117 double[] point = new double[3];118 point[0] = ((double[])from)[0]-200000;119 point[1] = ((double[])from)[1]-200000;120 point[2] = 0;121 122 return point;123 }124 125 /// <summary>更新线和标记的颜色,标记的文字大小126 /// </summary>127 private void updateColor()128 {129 foreach (AcadEntity entity in msLineAnno)130 {131 if (entity is AcadText)132 {133 AcadText text = (AcadText)entity;134 text.color = ACAD_COLOR.acRed;135 text.Height = 3.5;136 }137 else if (entity is AcadLWPolyline)138 {139 AcadLWPolyline line = (AcadLWPolyline)entity;140 line.color = ACAD_COLOR.acBlue;141 }142 else if (entity is AcadLine)143 {144 AcadLine line = (AcadLine)entity;145 line.color = ACAD_COLOR.acBlue;146 }147 else if (entity is AcadPoint)148 {149 AcadPoint point = (AcadPoint)entity;150 point.color = ACAD_COLOR.acGreen;151 }152 else if (entity is AcadSpline)153 {154 AcadSpline line = (AcadSpline)entity;155 line.color = ACAD_COLOR.acBlue;156 }157 else if (entity is AcadPolyline)158 {159 AcadPolyline line = (AcadPolyline)entity;160 line.color = ACAD_COLOR.acBlue;161 }162 //MessageBox.Show(entity.ObjectName);163 }164 }165 166 /// <summary>将点标记添加到线标记图层167 /// </summary>168 private void AddPointAnnoToLineAnno(AcadModelSpace msLineAnno, AcadModelSpace msPointAnno)169 {170 foreach (AcadEntity entity in msPointAnno)171 {172 if (entity is AcadText)173 {174 AcadText text = (AcadText)entity;175 msLineAnno.AddText(text.TextString,getPoint(text.InsertionPoint),text.Height);176 }177 }178 }179 180 /// <summary>将线添加到线标记图层181 /// </summary>182 private void AddLineToLineAnno(AcadModelSpace msLineAnno,AcadModelSpace msLine)183 {184 foreach (AcadEntity entity in msLine)185 {186 if (entity is AcadLWPolyline)187 {188 AcadLWPolyline line = (AcadLWPolyline)entity;189 double[] fromPoints = (double[])line.Coordinates;190 int count = fromPoints.Length;191 double newCount = count * 1.5;192 int newCounti = (int)newCount;193 double[] points = new double[newCounti];194 195 //原entity中double数组为x,y,x,y196 //新Polyline要求点为x,y,z,x,y,z,此处z=0197 for (int i = 0; i < count; i++)198 {199 200 int index = i / 2;201 int j = index * 3;202 203 if (i % 2 == 0)204 {205 points[j] = fromPoints[i]-200000;206 points[j + 2] = 0;207 }208 else209 {210 points[j + 1] = fromPoints[i]-200000;211 }212 }213 msLineAnno.AddPolyline(points);214 }215 else if (entity is AcadLine)216 {217 AcadLine line = (AcadLine)entity;218 double[] startPoint = new double[3];219 double[] endPoint = new double[3];220 startPoint[0] = ((double[])line.StartPoint)[0] - 200000;221 startPoint[1] = ((double[])line.StartPoint)[1] - 200000;222 startPoint[2] = 0;223 endPoint[0] = ((double[])line.EndPoint)[0] - 200000;224 endPoint[1] = ((double[])line.EndPoint)[1] - 200000;225 endPoint[2] = 0;226 msLineAnno.AddLine(startPoint, endPoint);227 }228 }229 }230 231 /// <summary>将点添加到线标记图层232 /// </summary>233 private void AddPointToLineAnno(AcadModelSpace msLineAnno, AcadModelSpace msPoint)234 {235 foreach (AcadEntity entity in msPoint)236 {237 if (entity is AcadPoint)238 {239 AcadPoint point = (AcadPoint)entity;240 double[] fromPoint = (double[])point.Coordinates;241 double[] topoint = new double[3];242 topoint[0] = fromPoint[0]-200000;243 topoint[1] = fromPoint[1] - 200000;244 topoint[2] = 0;245 msLineAnno.AddPoint(topoint); ;246 }247 }248 }249 }250 }
3 修改字体
AcadTextStyle style=docLineAnno.TextStyles.Add("宋体");//style.fontFile="C:/WINDOWS/Fonts/simsun.ttc"; // 程序在其他地方使用时,生成的cad文件打开时,可能会需要选择字体style.SetFont("宋体",false,false,0,0); //cad文件打开时,不需要选择字体//style.fontFile="C:/Programe Files/AutoCAD 2004/gbcbig.shx"; // 程序在其他地方使用时,生成的cad文件打开时,可能会需要选择字体docLineAnno.ActiveTextStyle=style; foreach(AcadEntity entity in msLineAnno){if(entity is AcadText) { AcadText text=(AcadText)entity; text.StyleName="宋体"; text.color=ACAD_COLOR.acByLayer; text.Height=3.5; text.ScaleFactor=0.75; }}
参考:
创建和编辑AutoCAD 图元
转载于:https://www.cnblogs.com/myparamita/archive/2012/02/15/2352704.html
C# AutoCAD ActiveX 二次开发 -- CAD图层合并相关推荐
- autocad python二次开发_基于Python AutoCAD ActiveX 二次开发,pyautocad应用技术
AutoCAD应用程序开发系列 基于Python AutoCAD ActiveX 二次开发技术 主要库:pyautocad==0.2.0 内容#:基于Python AutoCAD ActiveX 二次 ...
- 基于Python AutoCAD ActiveX 二次开发,pyautocad应用技术
AutoCAD应用程序开发系列 基于Python AutoCAD ActiveX 二次开发技术 主要库:pyautocad==0.2.0 内容#:基于Python AutoCAD ActiveX 二次 ...
- 利用C#进行AUTOCAD的二次开发
众所周知,对AutoCAD进行二次开发用到的主要工具有:ObjectArx,VBA,VLisp.但它们的优缺点是显而易见的:ObjectArx功能强大,编程效率高,但它的缺点是编程者必须掌握VC++, ...
- 利用C#进行AutoCAD的二次开发(转)
众所周知,对AutoCAD进行二次开发用到的主要工具有:ObjectArx,VBA,VLisp.但它们的优缺点是显而易见的:ObjectArx功能强大,编程效率高,但它的缺点是编程者必须掌握VC++, ...
- lisp 焊缝_焊接领域中 AutoCAD的二次开发
焊接领域中 AutoCAD 的二次开发 祁文军 ; 方建疆 ; 袁逸萍 ; 周建平 [摘 要] 应用 AutoCAD 已有的二次开发工具 :AutoLISP .块和形定义及菜单开发 对 CAD 在焊接 ...
- 利用C 进行AutoCAD的二次开发
利用C#进行AutoCAD的二次开发(一) 呵呵,把自己前在网上以表的文章放到这里. 利用C#进行AutoCAD的二次开发(一) 众所周知,对AutoCAD进行二次开发用到的主要工具有:ObjectA ...
- 《深入浅出AutoCAD.NET二次开发》教程(含光盘)
<深入浅出AutoCAD.NET二次开发>教程(含光盘)-中国建筑工业出版社出版 一本系统介绍.NET环境二次开发AutoCAD的教程,适合工程技术人员作为入门读物也适合研发工程师作为工作 ...
- 利用C#进行AutoCAD的二次开发(二)
大家好,今天我继续给各位介绍利用C#进行AutoCAD的二次开发.在这一讲中,主要介绍上一讲例子中存在的问题. 在上一次的例子中我是通过引用AutoCAD 2004 Type Library来进行C# ...
- C#进行MapX二次开发之图层操作
特别说明,本文整理自一篇网络的文章<MapX从数据库读取数据形成新图层(C#)> 在C#中实现MapX从数据库读取数据形成新图层分为两个问题: 1. MapX从数据库读取数据形成新图层: ...
最新文章
- numpy中的一些常见函数
- Python-函数的各种器
- ASP.NET MVC使用Bootstrap系统(2)——使用Bootstrap CSS和HTML元素
- poj2002 poj3432 正方形个数 (hash,二分)
- C++中的动态分配内存
- [Head First Java] - Swing做一个简单的客户端
- PHP 文件加密Zend Guard Loader 学习和使用(如何安装ioncube扩展对PHP代码加密)
- SAP License:SAP问题二则
- java.lang.InstantiationException: DWR can't find a spring config. See the logs for solutions
- python tornado websocket_Python:Tornado 第三章:WebSocket概念及应用:第一节:WebSocket概念...
- 使用ajax请求cgi,Python CGI同步AJAX请求
- 【数据分析师---数据可视化】第二章:plotly绘图进阶篇(地图可视化,动态数据可视化)
- 占鳌头。当雪花沁透着
- 题目:离骚-原文和译文少了一行,你能写程序识别并找出来吗
- matlab-计算个人所得税
- BIM模型一键输出 3D Tiles (for Cesium) 和 glTF/blg
- 使用ICMP协议检测网络状态
- tensorflow运行mnist例程第99次迭代cuda出错
- Linux配置环境变量不起作用
- java 调用天气预报接口_java调用天气预报接口案例
热门文章
- excel 打印 ,打印预览每一页只有一个单元格的问题
- python pandas fillna
- html和css画梯形背景,CSS-实战-梯形背景导航
- 怎么利用源码快速建立网站
- 再谈出版社与当当网打折之争--出版社暴利、书价高、7折销售有理、垃圾书太多
- java版哆啦a梦游戏_GitHub - wwz223/adventures-doraemon: 基于java实现的窗口类小游戏哆啦A梦历险记...
- TP5.1 captcha 验证码刷新不出来(图片显示不了)
- c语言全局变量控制打印线宽的方法,CAD中怎么控制图形的打印线宽
- python中矩阵转置4种方法
- iOS反编译-hook微信之艾特所有人