This is a tryout to write a command to add ticks at the end and start points of lwpolylines, still have some questions about how to insert an existing block in C#.
[CommandMethod("ADDTICKS")]
static public void addticks()
{
Document doc = acadApp.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
Database db = doc.Database;
PromptSelectionOptions prOpts = new PromptSelectionOptions();
prOpts.MessageForAdding = "\nSelect polylines: ";
TypedValue[] filter = new TypedValue[1];
Object obj = "LWPOLYLINE";
filter[0] = new TypedValue(0, obj);
SelectionFilter ssFilter = new SelectionFilter(filter);
PromptSelectionResult res = ed.GetSelection(prOpts, ssFilter);
if (res.Status != PromptStatus.OK) return;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
ObjectId[] ids = res.Value.GetObjectIds();
foreach (ObjectId id in ids)
{
try
{
Curve pline = tr.GetObject(id, OpenMode.ForRead, false) as Curve;
double end_param = pline.EndParam;
double start_param = pline.StartParam;
Point3d a, b;
a = pline.GetPointAtParameter(start_param);
b = pline.GetPointAtParameter(start_param + 1);
Vector3d v;
v = a - b;
Plane plane;
plane = new Plane(new Point3d(0, 0, 0), new Vector3d(0, 0, 1));
double ang1 = v.AngleOnPlane(plane) / Math.PI * 180;
double[] pin = new double[2];
pin[0] = a.X;
pin[1] = a.Y;
pin[3] = a.Z;
// the insertion part will go here...
b = pline.GetPointAtParameter(end_param);
a = pline.GetPointAtParameter(end_param - 1);
v = a - b;
plane = new Plane(new Point3d(0, 0, 0), new Vector3d(0, 0, 1));
double ang2 = v.AngleOnPlane(plane) / Math.PI * 180;
// the insertion part will go here...
} catch{}
}
tr.Commit();
}
}
I had tried:
Autodesk.AutoCAD.Interop.AcadApplication objAcadApp = (Autodesk.AutoCAD.Interop.AcadApplication Autodesk.AutoCAD.ApplicationServices.Application.AcadApplication;
Autodesk.AutoCAD.Interop.Common.AcadBlockReference objBlockRef = objAcadApp.ActiveDocument.ModelSpace.InsertBlock((object)pin, "TICK", 1, 1, 1, 0, "");
And...
ObjectId idblk = doc.Database.Insert("TICK", db, true);
BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead,true);
BlockTableRecord btr = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace],OpenMode.ForWrite);
BlockReference bref;
bref = new BlockReference(a, idblk);
btr.AppendEntity(bref);
tr.AddNewlyCreatedDBObject(bref, true);
I will continue later, and see if I can make this work.