VBA
Discuss AutoCAD ActiveX and VBA (Visual Basic for Applications) questions here.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How To insert Block in AutoCAD 2013 using vb.net

2 REPLIES 2
Reply
Message 1 of 3
AjitJoshi
2508 Views, 2 Replies

How To insert Block in AutoCAD 2013 using vb.net

I want a insert a block "C:\Cwtgov.dwg" using vb.net in autocad 2013 plug-ins. Can anybody give me sample code to do this. I was using InsertBlock method in VBA which was very simple. But in autoCAD 2013 plug-ins it appears to be bit tedious.

2 REPLIES 2
Message 2 of 3
Anonymous
in reply to: AjitJoshi

Here you go. But if you are trying to make it easy, try the lisp routine. it is easier to load and use.

 

VBA

Set ObjDocument = ThisDrawing.Application.Documents.Open("c:\Cwtgov.dwg")
For Each ObjBlock In ObjDocument.Blocks
If InStr(ObjBlock.Name, "Block_Name") <> 0 Then
CopyObjects 
End If
Next

 Lisp

(defun c:Cwtgov()
(command "insert" "*c:\\Cwtgov.dwg" (getpoint "\n Select insertion point: ") "" "" "0" )
)
(princ)

Hope this helps. let me know if you are having problems loading them in.

Message 3 of 3
Anonymous
in reply to: AjitJoshi

I think you know how to do it in VBA

 

Here is what I have to do it in VB .NET

You can cut alot of this out as I have written this function to cover many uses.

NOTE: I have edited it out some unimportant code but just in case check it works

 

Im sure others can supply something better

 

 

'Written by Tim Nell
'various error checking added

 Dim db As Database = AcadApplication.DocumentManager.MdiActiveDocument.Database
    Dim tm As Autodesk.AutoCAD.DatabaseServices.TransactionManager = db.TransactionManager

 Public Function InsertBlock(ByVal Ipoint As Point3d, ByVal BlockName As String, Optional Space As ObjectId = Nothing, Optional FileSearchPath As String = "") As ObjectId



        If Space = Nothing Then Space = db.CurrentSpaceId
        If FileSearchPath = "" Then FileSearchPath = Application.GetSystemVariable("DWGPREFIX")

        Try
            Using trans As Transaction = tm.StartTransaction()

                '-- Get Current space
                Dim btr As BlockTableRecord = trans.GetObject(Space, OpenMode.ForWrite)
                '--get block id
                Dim Blocks As BlockTable = trans.GetObject(db.BlockTableId, OpenMode.ForRead, False)
                Dim BnameID As ObjectId = Nothing
                Try
                    BnameID = Blocks(BlockName)
                Catch ex As Exception
                    BnameID = LoadBlockFromFile(FileSearchPath, BlockName)
                End Try

                '--create new block
                Dim NBlock As New BlockReference(Ipoint, BnameID)
                Dim NblockId As ObjectId = btr.AppendEntity(NBlock)
                trans.AddNewlyCreatedDBObject(NBlock, True)

                trans.Commit()
                trans.Dispose()

            End Using

        Catch ex As Exception
            MsgBox("Error Inserting Block:" & ex.Message)

            Try

            Catch ex2 As Exception
                MsgBox("Unable to find Block " & BlockName)
            End Try

        End Try

    End Function

 

.net

 

 

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost