Results 1 to 3 of 3

Thread: Insert DWG in DWG via vb.Net

  1. #1
    I could stop if I wanted to
    Join Date
    2004-06
    Location
    Port Moody, BC
    Posts
    349
    Login to Give a bone
    0

    Default Insert DWG in DWG via vb.Net

    I have the following where BK2 is a filename including path:

    Code:
    Try
                Dim destName As String = SymbolUtilityServices.GetSymbolNameFromPathName(BK2, "dwg")
                destName = SymbolUtilityServices.RepairSymbolName(destName, False)
                ' Create a source database to load the DWG into
                Using db As New Database(False, True)
                    ' Read the DWG into our side database
                    db.ReadDwgFile(BK2, FileOpenMode.OpenTryForReadShare, True, "")
                    ' Insert it into the destination database as
                    ' a named block definition
                    Dim btrId As ObjectId = destDb.Insert(destName, db, False)
                    ' If an annotative block, open the resultant BTR
                    ' and set its annotative definition status
                    Dim tr As Transaction = destDb.TransactionManager.StartTransaction()
                    Using tr
                        Dim btr As BlockTableRecord = DirectCast(tr.GetObject(btrId, OpenMode.ForWrite), BlockTableRecord)
                        tr.Commit()
                    End Using
                End Using
            Catch ex As System.Exception
                ed.WriteMessage(vbLf & "Problem importing ""{0}"": {1} - file skipped.", BK2, ex.Message)
            End Try
    
    OR EVEN
    
            Dim BK2 As String = "F:\" & "Somepath" & ".dwg"
            Dim doc As Document = DocumentManager.MdiActiveDocument
            
            Dim tCmd As String = "-INSERT" & vbCr & BK2 & vbCr
    
            doc.SendStringToExecute(tCmd, True, True, True)
    I get a crash on:

    Dim btrId As ObjectId = destDb.Insert(destName, db, False) in the 1st item

    OR I get a crash during the sendstringtoexecute in the 2nd, I am lost to what is going on...

    the error is eLockViolation - File Skipped

    Any ideas?
    Last edited by drewj; 2012-12-01 at 07:37 AM. Reason: Added code tags

  2. #2
    AUGI Addict fixo's Avatar
    Join Date
    2005-05
    Location
    Pietari, Venäjä
    Posts
    1,269
    Login to Give a bone
    0

    Default Re: Insert DWG in DWG via vb.Net

    You can try this code, it's very basic but will do your work
    Change drawing name on whatever you need before

    Code:
            <CommandMethod("cpy")> _
            Public Shared Sub testCopyDwg() ' This method can have any name 
    
                Dim doc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
    
                Dim ed As Editor = doc.Editor
    
                Dim db As Database = doc.Database
    
                Using tr As Transaction = db.TransactionManager.StartTransaction()
                    Dim bt As BlockTable = TryCast(tr.GetObject(db.BlockTableId, OpenMode.ForRead), BlockTable)
    
                    Dim btr As BlockTableRecord = TryCast(tr.GetObject(bt(BlockTableRecord.ModelSpace), OpenMode.ForWrite), BlockTableRecord)
    
                    Using newdb As New Database(True, False)
    
                        newdb.ReadDwgFile("C:\Test\WorkingDrawing.dwg", FileOpenMode.OpenForReadAndAllShare, False, "")
    
    
                        db.Insert(Matrix3d.Displacement(New Vector3d(0, 0, 0)), newdb, True)
                    End Using
    
                    tr.Commit()
                End Using
    
            End Sub

  3. #3
    I could stop if I wanted to
    Join Date
    2004-06
    Location
    Port Moody, BC
    Posts
    349
    Login to Give a bone
    0

    Default Re: Insert DWG in DWG via vb.Net

    Thanks Fixo, that worked great!

Similar Threads

  1. Map Insert VS FDO
    By jenniferchavez in forum AutoCAD Map 3D - Data Connect (FDO)
    Replies: 1
    Last Post: 2010-01-12, 11:08 PM
  2. Insert Sheet List - Won't insert
    By chris.klinert in forum ACA General
    Replies: 2
    Last Post: 2008-12-18, 03:45 PM
  3. How can I insert a PDF into a DWG
    By vinayan in forum AutoCAD General
    Replies: 3
    Last Post: 2007-08-01, 12:56 PM
  4. Insert OLE
    By a.r.t.studio in forum AutoCAD General
    Replies: 2
    Last Post: 2005-07-07, 10:59 PM
  5. Name that insert
    By Maverick91 in forum AutoCAD General
    Replies: 2
    Last Post: 2005-06-14, 01:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •