Jump to content

Inserting A Block in VB.NET


Recommended Posts

Something like this perhaps?


Pseudo code:

[color=black]     <CommandMethod("InsertBlock")> _
   Public Sub InsertBlock()
       Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
       acDoc.SendStringToExecute("._-insert [color=red]<YouFillInTheRest>[/color]", False, False, False)
   End Sub[/color]

Edited by BlackBox
Wow was my original post completely wrong!? That's what I get for posting VB.NET (from memory) from my iPhone!
Link to comment
Share on other sites

Here is an example of inserting a block reference by first selecting the file with a dialog box.


It is moderately basic; for instance, it could be extended further, via “jigging”, to allow visual feedback during the insert process. That would increase the complexity of the quite a bit.



Watch word wrap!


        Public Sub InsertFromFileCommand()
           Dim ofd As OpenFileDialog = New OpenFileDialog()
           ofd.DefaultExt = ".dwg"
           ofd.Filter = "Drawing Files (*.dwg)|*.dwg"
           If ofd.ShowDialog() <> DialogResult.OK Then
           End If
           Dim doc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
           Dim db As Database = doc.Database
           Dim ed As Editor = doc.Editor
           Dim ppo As New PromptPointOptions(vbLf & "Insertion point: ")
           Dim ppr As PromptPointResult = ed.GetPoint(ppo)
           If ppr.Status <> PromptStatus.OK Then
           End If
           Using xDb As New Database(False, True)
               xDb.ReadDwgFile(ofd.FileName, FileShare.Read, True, Nothing)
               Using tr As Transaction = doc.TransactionManager.StartTransaction()
                   Dim name As String = SymbolUtilityServices.GetBlockNameFromInsertPathName(ofd.FileName)
                   Dim id As ObjectId = db.Insert(name, xDb, True)
                   If id.IsNull Then
                       ed.WriteMessage(vbLf & "Failed to insert block")
                   End If
                   Dim currSpace As BlockTableRecord = CType(tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite), BlockTableRecord)
                   Dim p3d As Point3d = ppr.Value
                   Dim coordS As CoordinateSystem3d = New CoordinateSystem3d(p3d, db.Ucsxdir, db.Ucsydir) 'Determine UCS
                   Dim insert As New BlockReference(p3d, id)
                   insert.Normal = coordS.Zaxis 'Align to UCS
                   tr.AddNewlyCreatedDBObject(insert, True)
               End Using
           End Using
       End Sub

Edited by SEANT
Link to comment
Share on other sites

  • 10 years later...

Hi Seant,

I get some part of your code for my own program and it work as expect. The code was very usefull and was the best example to improve my code in the future.

Thank you.



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.

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