PDA

View Full Version : Insert Block with VB.NET



-KarL-
28th Sep 2009, 11:54 pm
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-

gile
29th Sep 2009, 05:28 am
Hi,

You can have a look at this tutorial:
http://images.autodesk.com/adsk/files/autocad_2007_.net_training.zip

or here:
http://docs.autodesk.com/ACD/2010/ENU/AutoCAD%20.NET%20Developer%27s%20Guide/index.html

-KarL-
5th Oct 2009, 03:40 pm
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.

gile
5th Oct 2009, 07:00 pm
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.CurrentUserCoordinateSyst em);

// 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();
}
}
}
}
}

gile
5th Oct 2009, 08:17 pm
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();
}
}
}
}
}

-KarL-
8th Oct 2009, 01:30 am
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-

gile
8th Oct 2009, 06:20 am
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)

shirazbj
23rd Feb 2012, 03:40 am
thx. know how to insert now.

xdbk07
28th Feb 2013, 03:00 am
Hi gile,

I want to select block by click it and replace some attribute value (using Jig). Please let me know how to do?

Thanks,