.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Inserting a preexisting drawing into a current drawing

4 REPLIES 4
SOLVED
Reply
Message 1 of 5
Anonymous
745 Views, 4 Replies

Inserting a preexisting drawing into a current drawing

I'm trying to programatically insert a block from a pre-existing drawing into the current drawing a plugin is running on. To do that, I have a button on my C#.NET form call the following method

 

public void MakeAndInsertObject() //Method to add all windows and doors templates to drawing database for use later
{
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; //Stores the active document
Editor ed = doc.Editor; //Stores the document's editor
Database dtb = ed.Document.Database; //Stores the database from the editor

Transaction tr = dtb.TransactionManager.StartTransaction(); //Start a transaction with the document's database
DocumentLock docLock = doc.LockDocument();
using (tr)
using (docLock)
{
BlockTableRecord btr = (BlockTableRecord)tr.GetObject(dtb.CurrentSpaceId, OpenMode.ForWrite); //Opens the block table record so you can write to it
BlockTableRecord newBlockDef = new BlockTableRecord(); //Creates a new record in the block table
BlockTable blockTable = (BlockTable)tr.GetObject(dtb.BlockTableId, OpenMode.ForWrite); //Opens the block table so it can be written to

//Pointing new block to correct drawing file
newBlockDef.Name = "Door";
newBlockDef.PathName = "C:/Users/Administrator/Documents/All Code/clearspan-autocad-tools-development/Templates/locks/DOOR.dwg";
blockTable.Add(newBlockDef); //Adds the block table record to the block table

BlockReference newBlock = new BlockReference(new Point3d(0, 0, 0), newBlockDef.ObjectId); //Insert a block reference with the newly created block
btr.AppendEntity(newBlock); //Inserts the block into the current space (Model or Paper) via the block table record

//Updates the Transaction with all new database objects
tr.AddNewlyCreatedDBObject(newBlockDef, true);
tr.AddNewlyCreatedDBObject(newBlock, true);

tr.Commit(); //Applies all changes made as part of the current transaction
tr.Dispose();
}
}

 

The code fully executes but the block in my DOOR.dwg file does not appear at location (0, 0, 0) and I do not know why it doesn't

Tags (3)
4 REPLIES 4
Message 2 of 5
hgasty1001
in reply to: Anonymous

Hi,

 

Try learning from this post: Redifining a Block

 

Gaston Nunez

Message 3 of 5
BrianPruitt
in reply to: Anonymous

Assuming that the block does insert, just not at the location you expected, check the source drawing. Entities in that drawing will be offset from your insertion point by the amount they are away from origin.

As a quick test create a simple block in a drawing and make sure you pick a insertion point on one of the entities. I suggest you just create a circle at 0,0,0 and make it a block with an insertion point of 0,0,0. Use WBLOCK to export it.

Open it in a new Acad instance and you will see it is simply centered abut origin. Move the circle to 1,0,0 and save. Now in another drawing insert a block, browse to the file and insert at 0,0,0. You will find that it is actually centered at 1,0,0.

Blocks insert relative to their origin.
Message 4 of 5
SENL1362
in reply to: Anonymous

This might help you to understand how to add and insert external blocks(drawings)

       public static ObjectId AddDwg(Database db, string dwgPathname)
        {
            ObjectId dwgBlkId = ObjectId.Null;

            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
                string blockName = Path.GetFileNameWithoutExtension(dwgPathname);
                if (bt.Has(blockName))
                    dwgBlkId = bt[blockName];
                else
                {
                    if (!File.Exists(dwgPathname))
                        return ObjectId.Null;
                    using (Database dwgDb = new Database(false, true))
                    {
                        dwgDb.ReadDwgFile(dwgPathname, FileShare.Read, true, null);
                        dwgDb.CloseInput(true);
                        dwgBlkId = db.Insert(blockName, dwgDb, true);
                        if (dwgBlkId == ObjectId.Null)
                            return ObjectId.Null;
                    }
                }
                tr.Commit();
            }
            return dwgBlkId;
        }

 

 

        /// <summary>
        /// InsertBlk(MsId, @"C:\x\y\BlockName.dwg",new Point3d(0,0,0),1,0);
        /// </summary>
        public static ObjectId InsertBlk(ObjectId blockTableRecordId, string blockPathName, Point3d insPnt, double insScale, double insRotation)
        {
            ObjectId blkRefId = ObjectId.Null;

            if (blockTableRecordId.IsNull)
                return ObjectId.Null;

            Database db = blockTableRecordId.Database;
            if (db == null)
                return ObjectId.Null;

            if (string.IsNullOrWhiteSpace(blockPathName))
                return ObjectId.Null;

            ObjectId blkId = AddDwg(db, blockPathName);
            if (blkId.IsNull)
                return ObjectId.Null;

            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                BlockTableRecord btr = (BlockTableRecord)tr.GetObject(blockTableRecordId, OpenMode.ForRead);
                BlockReference blkRef = new BlockReference(insPnt, blkId);
                btr.UpgradeOpen();
                btr.AppendEntity(blkRef);
                tr.AddNewlyCreatedDBObject(blkRef, true);

                if (insScale != 1)
                    blkRef.ScaleFactors = new Scale3d(insScale);

                if (insRotation != 0)
                    blkRef.Rotation = insRotation;

                foreach (ObjectId id in btr)
                {
                    DBObject obj = (DBObject)tr.GetObject(id, OpenMode.ForRead);
                    AttributeDefinition attDef = obj as AttributeDefinition;
                    if ((attDef != null) && (!attDef.Constant))
                    {
                        using (AttributeReference attRef = new AttributeReference())
                        {
                            attRef.SetAttributeFromBlock(attDef, blkRef.BlockTransform);
                            attRef.TextString = "";
                            blkRef.AttributeCollection.AppendAttribute(attRef);
                            tr.AddNewlyCreatedDBObject(attRef, true);
                        }
                    }
                }
                blkRefId = blkRef.ObjectId;
                tr.Commit();
            }
            return blkRefId;
        }

 

Message 5 of 5
SENL1362
in reply to: SENL1362

Oops! the recovery of the attributes must be changed into:

 

                BlockTableRecord newBlock = (BlockTableRecord)tr.GetObject(blkId, OpenMode.ForRead);
                foreach (ObjectId id in newBlock)

 

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost