Jump to content

code to insert dynamic block


kloe

Recommended Posts

hello

I need simple code to do just one thing, inserting a dynamic block that is stored in a separate file on my computer (like C:/base/b-22) in the drawing that I'm currently using. For example, I have a windows form with a list box with names of many dynamic blocks that are stored on my c drive, I would like to pick a block with a name and upon a click event of a button or other will go get this block from a separate file and insert it in the drawing I'm currently using, and would need to be able to pick the point of where I want the block to go. Could someone help, I'm using autocad 2010 with visual studio 2008- using the new autocad .net language.

Link to comment
Share on other sites

  • 4 weeks later...

Just to clone a block from another file and insert a reference would be the same if it was dynamic or not.

 

Here is link to a block manger I posted to get people started with one http://www.theswamp.org/index.php?topic=35280.0

 

Here is code from that link to bring it in and select a point to insert it.

It has it in VB to

 

[color=blue]private[/color] [color=blue]void[/color] InsertBlock([color=blue]string[/color] blkName, [color=blue]string[/color] fileName)
       {
           [color=#2b91af]Database[/color] extDb = [color=blue]new[/color] [color=#2b91af]Database[/color]([color=blue]false[/color], [color=blue]true[/color]) [color=blue]as[/color] [color=#2b91af]Database[/color];
           extDb.ReadDwgFile(fileName, [color=#2b91af]FileOpenMode[/color].OpenForReadAndAllShare, [color=blue]true[/color], [color=#a31515]""[/color]);
           [color=#2b91af]Document[/color] doc = [color=#2b91af]AcadApp[/color].DocumentManager.MdiActiveDocument;
           [color=#2b91af]Editor[/color] ed = doc.Editor;
           [color=#2b91af]Database[/color] db = doc.Database;
           [color=blue]using[/color] ([color=#2b91af]DocumentLock[/color] docloc = doc.LockDocument())
           [color=blue]using[/color] ([color=#2b91af]Transaction[/color] trx = db.TransactionManager.StartTransaction())
           [color=blue]using[/color] ([color=#2b91af]Transaction[/color] extTrx = extDb.TransactionManager.StartTransaction())
           {                
               [color=#2b91af]BlockTable[/color] bt = db.BlockTableId.GetObject([color=#2b91af]OpenMode[/color].ForRead) [color=blue]as[/color] [color=#2b91af]BlockTable[/color];

               [color=#2b91af]BlockTableRecord[/color] currBtr = db.CurrentSpaceId.GetObject([color=#2b91af]OpenMode[/color].ForRead) [color=blue]as[/color] [color=#2b91af]BlockTableRecord[/color];
               [color=blue]if[/color] (!(bt.Has(blkName)))
               {
                   [color=#2b91af]BlockTable[/color] extBt = extDb.BlockTableId.GetObject([color=#2b91af]OpenMode[/color].ForRead) [color=blue]as[/color] [color=#2b91af]BlockTable[/color];                    
                   [color=#2b91af]IdMapping[/color] map = [color=blue]new[/color] [color=#2b91af]IdMapping[/color]();
                   [color=#2b91af]ObjectIdCollection[/color] objIdColl = [color=blue]new[/color] [color=#2b91af]ObjectIdCollection[/color]();
                   [color=blue]try[/color]
                   {
                       objIdColl.Add(extBt[blkName]);
                   }
                   [color=blue]catch[/color]
                   {
                       trx.Commit();
                       [color=blue]return[/color];
                   }                    
                   db.WblockCloneObjects(objIdColl, bt.ObjectId, map, [color=#2b91af]DuplicateRecordCloning[/color].Replace, [color=blue]false[/color]);

               }               
                     [color=#2b91af]ObjectId[/color] btrId = bt[blkName].GetObject([color=#2b91af]OpenMode[/color].ForRead).ObjectId;

               [color=#2b91af]PromptPointOptions[/color] ppo = [color=blue]new[/color] [color=#2b91af]PromptPointOptions[/color]([color=#a31515]"/nSelect Insertion Point: "[/color]);
               [color=#2b91af]PromptPointResult[/color] ppr = ed.GetPoint(ppo);
               [color=blue]if[/color] (ppr.Status == [color=#2b91af]PromptStatus[/color].OK)
               {
                   [color=#2b91af]Point3d[/color] insertPnt = ppr.Value;
                   currBtr.UpgradeOpen();
                   [color=#2b91af]BlockReference[/color] bref = [color=blue]new[/color] [color=#2b91af]BlockReference[/color](insertPnt,btrId);
                   currBtr.AppendEntity(bref);
                   trx.AddNewlyCreatedDBObject(bref,[color=blue]true[/color]);
               }
               trx.Commit();
           }


       }

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...