Author Topic: Inserting a block into a existing block and getting block reference back in .NET  (Read 2027 times)

0 Members and 1 Guest are viewing this topic.

tik

  • Guest
I am converting a VBA project to C# and I am running into lot of issues in finding the right API calls in .NET for the old VBA calls. Here is one of that case and some one here can help me with this.

I have a block with has bunch of entities(line, arc etc..) in it. I want to insert a new block into this block and get block reference back. In VBA they are doing it by this code.

Set ThisNotchBlockRef = ThisBlock.InsertBlock(NotchInsPoint, ThisNotchBlock.Name, XScaleFactor, YScaleFactor, vdblPartScaleFactor, 0)

I want to know the equivalent functionality in C#.

Thanks in advance.

kaefer

  • Guest
Code: [Select]
Set ThisNotchBlockRef = ThisBlock.InsertBlock(NotchInsPoint, ThisNotchBlock.Name, XScaleFactor, YScaleFactor, vdblPartScaleFactor, 0)
I want to know the equivalent functionality in C#.

Welcome to the Swamp.

This is a somewhat awkward question, and any answer wouldn't pertain exclusively to C#, but to VB.NET or F# or whatever CLR language you happen to use as well.  First note that you generally don't pass object references around, but only their indentifiers, which are of type ObjectId. Then there's the need to access those drawing objects through an Transaction, keeping modifications of them localized between the opening "ForWrite" and a Commit() call on the Transaction.

That being said, it's certainly possibly to mimic the VBA function InsertBlock; it's just that such an approach wouldn't be particularly idiomatic. Like this:
Code: [Select]
        static ObjectId InsertBlock(
            ObjectId ThisBlockId,
            Point3d InsertionPoint,
            string Name,
            double Xscale,
            double Yscale,
            double Zscale,
            double Rotation)
        {
            Database db = ThisBlockId.Database;
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
                using (BlockReference br = new BlockReference(Point3d.Origin, bt[Name])
                    {
                        ScaleFactors = new Scale3d(Xscale, Yscale, Zscale),
                        Rotation = Rotation
                    })
                {
                    BlockTableRecord btr = (BlockTableRecord)tr.GetObject(ThisBlockId, OpenMode.ForWrite);
                    ObjectId brId = btr.AppendEntity(br);
                    tr.AddNewlyCreatedDBObject(br, true);
                    tr.Commit();
                    return brId;
                }
            }
        }

tik

  • Guest
Kaefer,

Thanks for taking time and explaining it to me. I will keep in mind to return objectids instead of returning entities.

regards
Tik.