AutoCAD2013 以上利用AccoreConsole+ c# NetApi 批量处理图纸

1# 先写个cmd脚本调用AccoreConsole

1 set acadPath="C:\Program Files\Autodesk\AutoCAD 2019\"
2 
3 %acadPath%accoreconsole.exe /i %~dp0Drawing1.dwg /s %~dp0NetCmd.scr
4 
5 @pause

多个dwg文件执行需要改成如下

1 set acadPath="C:\Program Files\Autodesk\AutoCAD 2019\"
2 
3 for %%i in (*.dwg) do %acadPath%accoreconsole.exe /i %~dp0%%i /s %~dp0NetCmd.scr
4 
5 @pause

2# 写个scr文件,命名为NetCmd.scr

netload "D:/MyProgrammingData/ShopDrawing/ShopDrawing/bin/Debug/ShopDrawing.dll"
(command "myNetcmd")
qsave
(setvar filedia 0)

3# 编写c#代码,生成的dll为ShopDrawing.dll,注册cad命令“myNetcmd”

skd的dll只能引用这三个

AutoCAD2013 以上利用AccoreConsole+ c# NetApi 批量处理图纸

 

 1 using AcApp = Autodesk.AutoCAD.ApplicationServices;
 2 using Autodesk.AutoCAD.Runtime;
 3 using System;
 4 using System.Collections.Generic;
 5 using System.Linq;
 6 using System.Text;
 7 using System.Threading.Tasks;
 8 using Autodesk.AutoCAD.DatabaseServices;
 9 using Autodesk.AutoCAD.ApplicationServices;
10 using Autodesk.AutoCAD.Geometry;
11 namespace ShopDrawing
12 {
13     public class AccoreCmd
14     {
15         [CommandMethod("myNetcmd")]
16         public void MyPlotModelSpace()
17         {
18             var doc = AcApp.Application.DocumentManager.MdiActiveDocument;
19             var db = HostApplicationServices.WorkingDatabase;
20             var ed = doc.Editor;
21             //转化成世界坐标系
22             if (ed.CurrentUserCoordinateSystem != Matrix3d.Identity) ed.CurrentUserCoordinateSystem = Matrix3d.Identity;
23             using (Transaction trans = db.TransactionManager.StartTransaction())
24             {
25                 BlockTable bt = trans.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
26                 BlockTableRecord ms = trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
27                 //获取块的边界范围
28                 foreach (ObjectId oid in ms)
29                 {
30                     Entity ent = trans.GetObject(oid, OpenMode.ForRead) as Entity;
31                     if (ent != null && ent.GetType().Name == typeof(BlockReference).Name)
32                     {
33                         BlockReference blkRef = ent as BlockReference;
34                         Polyline pline = new Polyline();
35                         pline.AddVertexAt(0, new Point2d(blkRef.GeometricExtents.MaxPoint.X, blkRef.GeometricExtents.MaxPoint.Y), 0, 0, 0);
36                         pline.AddVertexAt(0, new Point2d(blkRef.GeometricExtents.MinPoint.X, blkRef.GeometricExtents.MaxPoint.Y), 0, 0, 0);
37                         pline.AddVertexAt(0, new Point2d(blkRef.GeometricExtents.MinPoint.X, blkRef.GeometricExtents.MinPoint.Y), 0, 0, 0);
38                         pline.AddVertexAt(0, new Point2d(blkRef.GeometricExtents.MaxPoint.X, blkRef.GeometricExtents.MinPoint.Y), 0, 0, 0);
39                         pline.AddVertexAt(0, new Point2d(blkRef.GeometricExtents.MaxPoint.X, blkRef.GeometricExtents.MaxPoint.Y), 0, 0, 0);
40                         ms.AppendEntity(pline);
41                         trans.AddNewlyCreatedDBObject(pline, true);
42                         //
43                     }
44                 }
45                 trans.Commit();
46             }
47         }
48     }
49 }

AutoCAD2013 以上利用AccoreConsole+ c# NetApi 批量处理图纸

 4# 执行结果如下,双击bat文件

 

AutoCAD2013 以上利用AccoreConsole+ c# NetApi 批量处理图纸

 

 

AutoCAD2013 以上利用AccoreConsole+ c# NetApi 批量处理图纸

上一篇:根据AutoCAD地形图建立ANSYS和Flac3D实体模型


下一篇:AutoCAD.NET二次开发:扩展数据之XData