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