Author Topic: Insert block from container  (Read 718 times)

0 Members and 1 Guest are viewing this topic.

Offline fixo

  • Full Member
  • ***
  • Posts: 135
  • Karma: +4/-0
  • Gender: Male
    • prefered language: C
    • Prog expertise: Good
    • View Profile
Insert block from container
« on: June 29, 2012, 10:12:31 PM »
       using System.IO;
------------------------------------
// based on code written by Fenton Webb
Code: [Select]
        [CommandMethod("insFrom")]
        public static void ExtBlockCopyTest()
        {
            ObjectId blkTblRecId = ObjectId.Null;
           // Change path of file and block name here
            InsertBlockFromContainer(@"C:\Test\TitleExample.dwg", "TITLEBLOCK", blkTblRecId);
        }



        public static void InsertBlockFromContainer(string Container, string blkName, ObjectId blkTblRecId)
        {
            // Find the file containing all the blocks

            if (!File.Exists(Container))
            {
                MessageBox.Show("Could not find file !");

                return;

            }

            // Read the database in memory

            Database sourceDb = new Database(false, true);

            sourceDb.ReadDwgFile(Container, FileOpenMode.OpenForReadAndAllShare, false, "");

            using (Transaction sourceTr = sourceDb.TransactionManager.StartTransaction())
            {
                // Find the ObjectId of the block we are interested in.

                BlockTable sourceBt = null;

                sourceBt = sourceTr.GetObject(sourceDb.BlockTableId, OpenMode.ForRead) as BlockTable;

                ObjectId sourceBtId;

                BlockTableRecord sourceBtr = null;

                if (!sourceBt.Has(blkName))
                {

                    MessageBox.Show("Block not found !");

                    return;

                }

                sourceBtr = sourceTr.GetObject(sourceBt[blkName], OpenMode.ForRead) as BlockTableRecord;

                sourceBtId = sourceBtr.ObjectId;

                Database curDoc = HostApplicationServices.WorkingDatabase;

                Editor ed = acApp.DocumentManager.MdiActiveDocument.Editor;

                // Create a new block table record in the current database.
                using (Transaction tr = curDoc.TransactionManager.StartTransaction())
                {

                    BlockTable destBt = null;

                    destBt = tr.GetObject(curDoc.BlockTableId, OpenMode.ForRead) as BlockTable;

                    ObjectId destBtId = destBt.ObjectId;

                    // WBlockClone the BlockTable record to the current database

                    ObjectIdCollection idColl = new ObjectIdCollection();

                    idColl.Add(sourceBtId);

                    // ObjectId of the btr that we want to clone

                    IdMapping idMap = new IdMapping();

                    curDoc.WblockCloneObjects(idColl, destBtId, idMap, DuplicateRecordCloning.Ignore, false);

                    // Create an instance of the block

                    BlockTableRecord destBtr = null;

                    destBtr = tr.GetObject(destBt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;

                    ObjectId destBtrId;

                    destBtrId = destBt[blkName];

                    // Insert an instance of the block in point(0,0,0)
                    BlockReference bref = new BlockReference(Point3d.Origin, destBtrId);

                    destBtr.AppendEntity(bref);

                    tr.AddNewlyCreatedDBObject(bref, true);
                    // Check, then insert the attributes
                    BlockTableRecord btrec = null;

                    btrec = tr.GetObject(bref.BlockTableRecord, OpenMode.ForRead) as BlockTableRecord;

                    if (btrec.HasAttributeDefinitions)
                    {
                        foreach (ObjectId id in btrec)
                        {
                            AttributeDefinition atdef = tr.GetObject(id, OpenMode.ForRead) as AttributeDefinition;

                            if (atdef != null)
                            {
                                using (AttributeReference atref = new AttributeReference())
                                {
                                    atref.SetAttributeFromBlock(atdef, bref.BlockTransform);

                                    atref.Position = atdef.Position + bref.Position.GetAsVector();

                                    atref.TextString = atdef.TextString;

                                    bref.AttributeCollection.AppendAttribute(atref);

                                    tr.AddNewlyCreatedDBObject(atref, true);
                                }
                            }
                        }
                    }

                    tr.Commit();
                }

            }
        }

~'J'~