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

AccoreConsole听起来有点拗口,其中文名可以叫做AutoCAD控制台或者无头AutoCAD。一句话概括,它是快速启动AutoCAD运行微环境,高效的处理图纸。你可以如同DOS命令行一样操作命令,处理图纸,而不需要打开AutoCAD。这在批处理大量图纸时非常有用。另外启动是不管有无license都可以正常操作,不占当前autoCAD的资源

安装AutoCAD后,在安装路径根目录下就可以看到这个可执行程序 AccoreConsole.exe



 

直入正题,Accoreconsole+scr+bat 纯命令只能单一机械的执行处理命令,今天笔者就结合C# AutoCAD NetApi 来探究下Accoreconsole的强大功能。。。。。。

1# 先写个cmd脚本调用AccoreConsole

%~dp0 代表当前脚本文件的文件夹路径,
/i 打开的图纸的全路径
/s 执行的scr文件的全路径
1 set acadPath="C:\Program Files\Autodesk\AutoCAD 2019\"
2 
3 %acadPath%accoreconsole.exe /i %~dp0Drawing1.dwg /s %~dp0NetCmd.scr
4 
5 @pause

多个dwg文件执行需要改成如下,请根据AutoCAD的安装路径更改成对应的版本

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

3 @pause

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

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


secureload 0
cmdecho 0
netload "C:\Users\nslov\Desktop\Test\ShopDrawing.dll"
filedia 0
(if (= (getvar "WRITESTAT") 0) (Command "close"))
mySSsetTest
qsave

 


 

解释部分:secureload 0, 关闭dll加载提示
cmdecho 0 关闭回显

(if (= (getvar "WRITESTAT") 0) (Command "close"))

只读的话就执行退出,非只读才执行后面对应的命令

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

skd的dll只能引用这2个dll,AcDbMgd.dll,AcCoreMgd.dll

这2个dll都可以用,选择集经测试可以使用


 

 1 using Autodesk.AutoCAD.Runtime;
 2 using System;
 3 using System.Collections.Generic;
 4 using System.Linq;
 5 using System.Text;
 6 using System.Threading.Tasks;
 7 using Autodesk.AutoCAD.DatabaseServices;
 8 using Autodesk.AutoCAD.ApplicationServices;
 9 using Autodesk.AutoCAD.Geometry;
10 using Autodesk.AutoCAD.EditorInput;
11 using Autodesk.AutoCAD.ApplicationServices.Core;
12 
13 namespace ShopDrawing
14 {
15     public class AccoreCmd
16     {
17         [CommandMethod("mySSsetTest")]
18         public void MyPlotModelSpace()
19         {
20             var doc = Application.DocumentManager.MdiActiveDocument;
21             var db = HostApplicationServices.WorkingDatabase;
22             var ed = doc.Editor;
23             //转化成世界坐标系
24             if (ed.CurrentUserCoordinateSystem != Matrix3d.Identity) ed.CurrentUserCoordinateSystem = Matrix3d.Identity;
25             using (Transaction trans = db.TransactionManager.StartTransaction())
26             {
27                 BlockTable bt = trans.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
28                 BlockTableRecord ms = trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
29                 //获取块的边界范围
30                 var psr = ed.SelectCrossingWindow(Point3d.Origin, new Point3d(350, 650, 0),
31                     new Autodesk.AutoCAD.EditorInput.SelectionFilter(new TypedValue[] {
32                         new TypedValue((int)DxfCode.Start, RXClass.GetClass(typeof(BlockReference)).DxfName)
33                         }));
34                 if (psr.Status == Autodesk.AutoCAD.EditorInput.PromptStatus.OK)
35                 {
36                     foreach (SelectedObject item in psr.Value)
37                     {
38                         Entity ent = trans.GetObject(item.ObjectId, OpenMode.ForRead) as Entity;
39                         if (ent != null)
40                         {
41                             BlockReference blkRef = ent as BlockReference;
42                             if (blkRef.Name == "123")
43                             {
44                                 Polyline pline = new Polyline();
45                                 pline.AddVertexAt(0, new Point2d(blkRef.GeometricExtents.MaxPoint.X, blkRef.GeometricExtents.MaxPoint.Y), 0, 0, 0);
46                                 pline.AddVertexAt(0, new Point2d(blkRef.GeometricExtents.MinPoint.X, blkRef.GeometricExtents.MaxPoint.Y), 0, 0, 0);
47                                 pline.AddVertexAt(0, new Point2d(blkRef.GeometricExtents.MinPoint.X, blkRef.GeometricExtents.MinPoint.Y), 0, 0, 0);
48                                 pline.AddVertexAt(0, new Point2d(blkRef.GeometricExtents.MaxPoint.X, blkRef.GeometricExtents.MinPoint.Y), 0, 0, 0);
49                                 pline.AddVertexAt(0, new Point2d(blkRef.GeometricExtents.MaxPoint.X, blkRef.GeometricExtents.MaxPoint.Y), 0, 0, 0);
50                                 ms.AppendEntity(pline);
51                                 trans.AddNewlyCreatedDBObject(pline, true);
52                             }
53                         }
54                     }
55                     trans.Commit();
56                 }
57                 else ed.WriteMessage("Can't Find Any Block");
58             }
59         }
60     }
61 }
View Code

 4# 执行结果如下,双击bat文件执行如下,bat 文件和scr都要仔细调试,一步错步步错!!!

 5# AutoCAD script脚本语法简介

  与cad命令行操作一样,命令行怎么输入,代码就怎写。。。

  1. 脚本文件空格作为命令或数据结束符来处理
  2. 脚本命令不要简写,以免歧义。
  3. 注释语法以分号表示。
  4. 最后一行必须为空行。
  5. 嵌入空格的长文件名用双引号括起来。 如open “my house”; 打开my house.dwg文件

转载于:https://www.cnblogs.com/NanShengBlogs/p/10957489.html

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值