Author Topic: AddTicks Command - insert block?  (Read 4818 times)

0 Members and 1 Guest are viewing this topic.

LE

  • Guest
AddTicks Command - insert block?
« on: December 05, 2006, 07:02:44 PM »
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#.

Code: [Select]
[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:

Code: [Select]
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...

Code: [Select]
                        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.

Glenn R

  • Guest
Re: AddTicks Command - insert block?
« Reply #1 on: December 05, 2006, 08:07:18 PM »
Luis,

Something along these lines:

Code: [Select]
BlockReference newBlkRef = new BlockReference(someInsertionPoint3D, existingBlockTableRecordObjectId);
someBlockTableRecordContainer.AppendEntity(newBlkRef);
someTransaction.AddNewlyCreatedDBObject(newBlkRef, true);

That should point you in the right direction.

Cheers,
Glenn.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: AddTicks Command - insert block?
« Reply #2 on: December 05, 2006, 09:07:09 PM »
You'll probably want to test if the block exists too,

perhaps grab something from this ...

Code: [Select]
    public class TestBlock
    {
        // kwb@theSwamp
        //
        [CommandMethod("TestBlock")]
        static public void DoIt()
        {
            const string BlockName = "TICK";

            Document doc = AcadApp.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;                     
           
            using (Transaction tr = doc.TransactionManager.StartTransaction())
            {
                BlockTable bt =
                    (BlockTable)tr.GetObject(db.BlockTableId,
                    OpenMode.ForRead, true);

                if (bt.Has(BlockName))
                {
                    BlockTableRecord blkRec =
                        (BlockTableRecord)tr.GetObject(bt[BlockName],
                        OpenMode.ForRead);

                    MessageBox.Show("Block exists.");
                }
                else
                {
                    MessageBox.Show("Block does NOT exists.");
                }
            }
        }
    }
« Last Edit: December 05, 2006, 09:09:14 PM by Kerry Brown »
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

LE

  • Guest
Re: AddTicks Command - insert block?
« Reply #3 on: December 06, 2006, 11:15:42 AM »
Thank you Glenn and Kerry

Here is the updated code:

Code: [Select]
[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;
                string blockname = "TICK";
                BlockTable BT = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
                if (BT.Has(blockname))
                {
                    //ed.WriteMessage("\nBlock exist...");
                    BlockTableRecord blkRec = (BlockTableRecord)tr.GetObject(BT[blockname], OpenMode.ForRead);
                    BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
                    BlockReference newBlkRef;
                    newBlkRef = new BlockReference(a, blkRec.ObjectId);
                    btr.AppendEntity(newBlkRef);
                    tr.AddNewlyCreatedDBObject(newBlkRef, true);
                    newBlkRef.Rotation = ((ang1 / 180.0) * Math.PI);
                    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;
                    newBlkRef = new BlockReference(b, blkRec.ObjectId);
                    btr.AppendEntity(newBlkRef);
                    tr.AddNewlyCreatedDBObject(newBlkRef, true);
                    newBlkRef.Rotation = ((ang2 / 180.0) * Math.PI);
                }
            } catch{}
        }
        tr.Commit();
    }
}

Have fun.

LE

  • Guest
Re: AddTicks Command - insert block?
« Reply #4 on: December 06, 2006, 11:29:26 AM »
To add the scale factor for the TICKS using the DIMSCALE, we can do:

Code: [Select]
Scale3d sc = new Scale3d(db.Dimscale); // uniform scale XYZ
newBlkRef.ScaleFactors = sc; // setup our scale for the block

Nice!

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: AddTicks Command - insert block?
« Reply #5 on: December 06, 2006, 04:47:20 PM »
To add the scale factor for the TICKS using the DIMSCALE, we can do:
...................

Nice!

Yes, I'm really getting to like this language ..... just a pity I can't make more time to work with it ... I keep telling myself 'next month' , yeah, right, sure thing !



kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

LE

  • Guest
Re: AddTicks Command - insert block?
« Reply #6 on: December 06, 2006, 05:04:13 PM »
Yes, I'm really getting to like this language ..... just a pity I can't make more time to work with it ... I keep telling myself 'next month' , yeah, right, sure thing !

 :-)

Yes.

But you at least have a job - I'm not, that's why I can play a lot, I just added the ability to work with splines and polylines with end arced segments (bulges), and it is working just fine and dandy!

I like the way we can do filters (as far my understanding it is until now)

Code: [Select]
    TypedValue[] filter = new TypedValue[1];
    Object obj = "LWPOLYLINE,SPLINE";
    filter[0] = new TypedValue(0, obj);
    SelectionFilter ssFilter = new SelectionFilter(filter);

Pretty simple, (it is also in ARX no biggy)

The only hard part was to understand the angle acquisition, since no acutAngle is available... but once you get it....  will see ... at least I am getting something to work!


Have fun.