|
发表于 2016-2-28 20:09:24
|
显示全部楼层
I've cleaned up a subtle bug in the GetDimBlockId method.
If the current DIMBLK value stored in the variable originalArrowName was the default 'Closed filled' the subsequent restoring of the DIMBLK variable may result in a crash.
The revised code is
[ol] // Restore the previous setting
if(string.IsNullOrEmpty(originalArrowName)) {
db.Dimblk = ObjectId.Null;
}
else {
AcadApp.SetSystemVariable(dimBlkX, originalArrowName);
}[/ol]
I've also allowed for the newArrowName parameter to have the value of "." in line with the AutoCAD command line option.
[ol] if(string.IsNullOrEmpty(newArrowName)
|| newArrowName.Equals(".")) {
return ObjectId.Null;
}
[/ol]
Revised code :
[ol]// Proof of concept code 2016-02-29
// codehimbelonga kdub @ theSwamp
using System;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using AcadApp = Autodesk.AutoCAD.ApplicationServices.Core.Application;
[assembly: CommandClass(typeof(DimBlocks.MyCommands))]
namespace DimBlocks {
public class MyCommands {
[CommandMethod("DoIt", CommandFlags.Modal)]
public void MyCommand() {
TM11();
AcadApp.DocumentManager.MdiActiveDocument.Editor.WriteMessage(
"\n\n ====================================\n");
TM12();
}
private static void TM11() {
var doc = AcadApp.DocumentManager.MdiActiveDocument;
var db = AcadApp.DocumentManager.MdiActiveDocument.Database;
var ed = doc.Editor;
var arrowName = string.Empty;
// Get the current DIMBLK setting
arrowName = (string)AcadApp.GetSystemVariable("DIMBLK");
ed.WriteMessage($"\nDIMBLK is: {arrowName} \nID is: {db.Dimblk}\n");
// Set to '_OPEN'
AcadApp.SetSystemVariable("DIMBLK", "_OPEN");
arrowName = (string)AcadApp.GetSystemVariable("DIMBLK");
ed.WriteMessage($"\nDIMBLK is: {arrowName} \nID is: {db.Dimblk}\n");
// Set to default 'Closed filled'
db.Dimblk = ObjectId.Null;
arrowName = (string)AcadApp.GetSystemVariable("DIMBLK");
ed.WriteMessage($"\nDIMBLK is: {arrowName} \nID is: {db.Dimblk}\n");
// Set to '_DOT'
AcadApp.SetSystemVariable("DIMBLK", "_DOT");
arrowName = (string)AcadApp.GetSystemVariable("DIMBLK");
ed.WriteMessage($"\nDIMBLK is: {arrowName} \nID is: {db.Dimblk}\n");
// Set to '.'
AcadApp.SetSystemVariable("DIMBLK", ".");
arrowName = (string)AcadApp.GetSystemVariable("DIMBLK");
ed.WriteMessage($"\nDIMBLK '.'is: {arrowName} \nID is: {db.Dimblk}\n");
}
private static void TM12() {
var doc = AcadApp.DocumentManager.MdiActiveDocument;
var db = AcadApp.DocumentManager.MdiActiveDocument.Database;
var ed = doc.Editor;
var arrowName = string.Empty;
var dimBlockId = ObjectId.Null;
arrowName = (string)AcadApp.GetSystemVariable("DIMBLK");
ed.WriteMessage($"\nDIMBLK is: {arrowName} \nID is: {db.Dimblk}\n");
dimBlockId = GetDimBlockId("DIMBLK", "_DOTBLANK");
db.Dimblk = dimBlockId;
arrowName = (string)AcadApp.GetSystemVariable("DIMBLK");
ed.WriteMessage($"\nDIMBLK is: {arrowName} \nID is: {dimBlockId}\n");
dimBlockId = GetDimBlockId("DIMBLK", ".");
db.Dimblk = dimBlockId;
arrowName = (string)AcadApp.GetSystemVariable("DIMBLK");
ed.WriteMessage($"\nDIMBLK '.' is: {arrowName} \nID is: {dimBlockId}\n");
dimBlockId = GetDimBlockId("DIMBLK", "");
db.Dimblk = dimBlockId;
arrowName = (string)AcadApp.GetSystemVariable("DIMBLK");
ed.WriteMessage($"\nDIMBLK '' is: {arrowName} \nID is: {dimBlockId}\n");
}
private static ObjectId GetDimBlockId(string dimBlkX, string newArrowName) {
ObjectId arrowObjId = ObjectId.Null;
if(string.IsNullOrEmpty(newArrowName)
|| newArrowName.Equals(".")) {
return ObjectId.Null;
}
var db = AcadApp.DocumentManager.MdiActiveDocument.Database;
var originalArrowName = (string)AcadApp.GetSystemVariable(dimBlkX);
// Force AutoCAD to add the block to the db.
AcadApp.SetSystemVariable(dimBlkX, newArrowName);
// Restore the previous setting
if(string.IsNullOrEmpty(originalArrowName)) {
db.Dimblk = ObjectId.Null;
}
else {
AcadApp.SetSystemVariable(dimBlkX, originalArrowName);
}
using(Transaction tr = db.TransactionManager.StartTransaction()) {
var bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
arrowObjId = bt[newArrowName];
tr.Commit();
}
return arrowObjId;
}
}
public class InitMyCommands : Autodesk.AutoCAD.Runtime.IExtensionApplication {
public void Initialize() {
AcadApp.DocumentManager.MdiActiveDocument.Editor.WriteMessage("\n\t\t\t*** type DOIT to run test \t***");
}
public void Terminate() {
AcadApp.DocumentManager.MdiActiveDocument.Editor.WriteMessage("Cleaning up...");
}
}
}
[/ol]
New piccy attached: |
|