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的安装路径更改成对应的版本
acadPath=" %~dp0 @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都可以用,选择集经测试可以使用
using Autodesk.AutoCAD.Runtime;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.ApplicationServices.Core;
namespace ShopDrawing
{
public class AccoreCmd
{
[CommandMethod("mySSsetTest")]
public void MyPlotModelSpace()
{
var doc = Application.DocumentManager.MdiActiveDocument;
var db = HostApplicationServices.WorkingDatabase;
var ed = doc.Editor;
//转化成世界坐标系
if (ed.CurrentUserCoordinateSystem != Matrix3d.Identity) ed.CurrentUserCoordinateSystem = Matrix3d.Identity;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
BlockTable bt = trans.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
BlockTableRecord ms = trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
//获取块的边界范围
var psr = ed.SelectCrossingWindow(Point3d.Origin, new Point3d(350, 650, 0),
new Autodesk.AutoCAD.EditorInput.SelectionFilter(new TypedValue[] {
new TypedValue((int)DxfCode.Start, RXClass.GetClass(typeof(BlockReference)).DxfName)
}));
if (psr.Status == Autodesk.AutoCAD.EditorInput.PromptStatus.OK)
{
foreach (SelectedObject item in psr.Value)
{
Entity ent = trans.GetObject(item.ObjectId, OpenMode.ForRead) as Entity;
if (ent != null)
{
BlockReference blkRef = ent as BlockReference;
if (blkRef.Name == "123")
{
Polyline pline = new Polyline();
pline.AddVertexAt(0, new Point2d(blkRef.GeometricExtents.MaxPoint.X, blkRef.GeometricExtents.MaxPoint.Y), 0, 0, 0);
pline.AddVertexAt(0, new Point2d(blkRef.GeometricExtents.MinPoint.X, blkRef.GeometricExtents.MaxPoint.Y), 0, 0, 0);
pline.AddVertexAt(0, new Point2d(blkRef.GeometricExtents.MinPoint.X, blkRef.GeometricExtents.MinPoint.Y), 0, 0, 0);
pline.AddVertexAt(0, new Point2d(blkRef.GeometricExtents.MaxPoint.X, blkRef.GeometricExtents.MinPoint.Y), 0, 0, 0);
pline.AddVertexAt(0, new Point2d(blkRef.GeometricExtents.MaxPoint.X, blkRef.GeometricExtents.MaxPoint.Y), 0, 0, 0);
ms.AppendEntity(pline);
trans.AddNewlyCreatedDBObject(pline, true);
}
}
}
trans.Commit();
}
else ed.WriteMessage("Can't Find Any Block");
}
}
}
}
4# 执行结果如下,双击bat文件执行如下,bat 文件和scr都要仔细调试,一步错步步错!!!
5# AutoCAD script脚本语法简介
与cad命令行操作一样,命令行怎么输入,代码就怎写。。。
脚本文件空格作为命令或数据结束符来处理
脚本命令不要简写,以免歧义。
注释语法以分号表示。
最后一行必须为空行。
嵌入空格的长文件名用双引号括起来。 如open “my house”; 打开my house.dwg文件
文章评论