Jump to content

Insert Block with VB.NET


-KarL-

Recommended Posts

I am finally getting around to converting my programs over to VB.NET and I have been able to find a lot of sample code to help out but the one thing I can't figure out is inserting blocks. Anyone have any sample code I can look at to try to figure this out.

 

Thanks,

-KarL-

Link to comment
Share on other sites

I have been using the autodesk site link a lot already, but haven't found anything on inserting blocks, unless I am looking in the wrong spot.

Link to comment
Share on other sites

Here's a little sample (C#, sorry, but quite easy to translate)

 

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;

namespace BlockInsertion
{
   public class Class1
   {
       [CommandMethod("Test")]
       public void Test()
       {
           Document doc = Application.DocumentManager.MdiActiveDocument;
           Database db = doc.Database;
           Editor ed = doc.Editor;

           // Get the block name
           PromptStringOptions pso = new PromptStringOptions("\nEnter block name: ");
           PromptResult pr = ed.GetString(pso);
           if (pr.Status == PromptStatus.OK)
           {
               // Start a transaction
               using (Transaction tr = db.TransactionManager.StartTransaction())
               {
                   // Test if block exists in the block table
                   BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
                   if (!bt.Has(pr.StringResult))
                   {
                       ed.WriteMessage("\nBlock not found.");
                   }
                   else
                   {
                       ObjectId id = bt[pr.StringResult];

                       // Get insertion point
                       PromptPointOptions ppo = new PromptPointOptions("\nSpecify insertion point: ");
                       PromptPointResult ppr = ed.GetPoint(ppo);
                       if (ppr.Status == PromptStatus.OK)
                       {
                           Point3d pt = ppr.Value.TransformBy(ed.CurrentUserCoordinateSystem);

                           // Create a block reference
                           BlockReference br = new BlockReference(pt, id);

                           // Get Model space
                           BlockTableRecord btr = (BlockTableRecord)tr.GetObject(bt[blockTableRecord.ModelSpace], OpenMode.ForWrite);

                           // Add the block reference to Model space
                           btr.AppendEntity(br);
                           tr.AddNewlyCreatedDBObject(br, true);
                       }
                   }

                   // Commit the transaction
                   tr.Commit();
               }
           }
       }
   }
}

Link to comment
Share on other sites

The same one using a Jig.

 

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;

namespace BlockInsertion
{
   class BlockJig : EntityJig
   {
       Point3d pt;
       public BlockJig(BlockReference br) : base(br)
       {
           pt = br.Position;
       }

       protected override SamplerStatus Sampler(JigPrompts prompts)
       {
           JigPromptPointOptions jpo = new JigPromptPointOptions("\nInsertion point: ");
           jpo.UserInputControls = UserInputControls.Accept3dCoordinates;
           PromptPointResult ppr = prompts.AcquirePoint(jpo);
           if (ppr.Status == PromptStatus.OK)
           {
               if (pt == ppr.Value)
               {
                   return SamplerStatus.NoChange;
               }
               else
               {
                   pt = ppr.Value;
                   return SamplerStatus.OK;
               }
           }
           return SamplerStatus.Cancel;
       }

       protected override bool Update()
       {
           ((BlockReference)Entity).Position = pt;
           return true;
       }
   }

   public class TestCommand
   {
       [CommandMethod("Test")]
       public void Test()
       {
           Document doc = Application.DocumentManager.MdiActiveDocument;
           Database db = doc.Database;
           Editor ed = doc.Editor;
           PromptStringOptions pso = new PromptStringOptions("\nEnter block name: ");
           PromptResult pr = ed.GetString(pso);
           if (pr.Status == PromptStatus.OK)
           {
               using (Transaction tr = db.TransactionManager.StartTransaction())
               {
                   BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
                   if (!bt.Has(pr.StringResult))
                   {
                       ed.WriteMessage("\nBlock not found.");
                   }
                   else
                   {
                       ObjectId id = bt[pr.StringResult];
                       BlockReference br = new BlockReference(new Point3d(0.0, 0.0, 0.0), id);
                       BlockJig bJig = new BlockJig(br);
                       pr = ed.Drag(bJig);
                       if (pr.Status == PromptStatus.OK)
                       {
                           BlockTableRecord btr = (BlockTableRecord)tr.GetObject(bt[blockTableRecord.ModelSpace], OpenMode.ForWrite);
                           btr.AppendEntity(br);
                           tr.AddNewlyCreatedDBObject(br, true);
                       }
                   }
                   tr.Commit();
               }
           }
       }
   }
}

Link to comment
Share on other sites

Any chance you can translate this into VB code?

ObjectId id = bt[pr.StringResult];

I think that is where I am having the problem

 

Thanks for the help so far

-KarL-

Link to comment
Share on other sites

Any chance you can translate this into VB code?

ObjectId id = bt[pr.StringResult];

I think that is where I am having the problem

 

Thanks for the help so far

-KarL-

 

Dim id As ObjectId = bt(pr.StringResult)

 

Or (maybe more explicit):

Dim blockName As string = pr.StringResult
Dim id As ObjectId = bt(blockName)

Link to comment
Share on other sites

  • 2 years later...
  • 1 year later...

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...