AutoCAD2013 以上利用AccoreConsole+ c# NetApi 批量處理圖紙

AccoreConsole聽起來有點拗口,其中文名能夠叫作AutoCAD控制檯或者無頭AutoCAD。一句話歸納,它是快速啓動AutoCAD運行微環境,高效的處理圖紙。你能夠如同DOS命令行同樣操做命令,處理圖紙,而不須要打開AutoCAD。這在批處理大量圖紙時很是有用。另外啓動是無論有無license均可以正常操做,不佔當前autoCAD的資源c#

安裝AutoCAD後,在安裝路徑根目錄下就能夠看到這個可執行程序 AccoreConsole.exeide



 

直入正題,Accoreconsole+scr+bat 純命令只能單一機械的執行處理命令,今天筆者就結合C# AutoCAD NetApi 來探究下Accoreconsole的強大功能。。。。。。測試

1# 先寫個cmd腳本調用AccoreConsolespa

%~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.scr3d

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
qsavecode

 


 

解釋部分:secureload 0, 關閉dll加載提示
cmdecho 0 關閉回顯blog

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

只讀的話就執行退出,非只讀才執行後面對應的命令

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文件
相關文章
相關標籤/搜索