-KarL- Posted September 28, 2009 Posted September 28, 2009 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- Quote
gile Posted September 29, 2009 Posted September 29, 2009 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 Quote
-KarL- Posted October 5, 2009 Author Posted October 5, 2009 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. Quote
gile Posted October 5, 2009 Posted October 5, 2009 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(); } } } } } Quote
gile Posted October 5, 2009 Posted October 5, 2009 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(); } } } } } Quote
-KarL- Posted October 8, 2009 Author Posted October 8, 2009 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- Quote
gile Posted October 8, 2009 Posted October 8, 2009 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) Quote
xdbk07 Posted February 28, 2013 Posted February 28, 2013 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, Quote
Recommended Posts
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.