How do I add objects to a drawing NOT open in Acad?

How do I add objects to a drawing NOT open in Acad?

jshimkus
Advocate Advocate
1,400 Views
6 Replies
Message 1 of 7

How do I add objects to a drawing NOT open in Acad?

jshimkus
Advocate
Advocate

I just want to open the drawings database and add objects to it then save and close the database.

The code I have is getting the drawings database, it appears to be commiting the objects, but the objects are not there when I open the drawing.

dbPath is a string path to an exisiting drawing file and I do 

myDB.Dispose() the drawing database at the end of the code.

Is it even possible to add objects to a drawing without actually opening it?

 

            Using myDB As Database = New Database(False, False)
                myDB.ReadDwgFile(dbPath, FileOpenMode.OpenForReadAndWriteNoShare, True, "")
                Dim vptLL As Point3d = New Point3d(0, 0, 0)
                Dim vptUL As Point3d = New Point3d(vptLL.X, 12, 0)
                Dim vptUR As Point3d = New Point3d(12, vptUL.Y, 0)
                Dim vptLR As Point3d = New Point3d(vptUR.X, vptLL.Y, 0)
                Dim ptcoll As New Point3dCollection

                ptcoll.Add(vptLL)
                ptcoll.Add(vptLR)
                ptcoll.Add(vptUR)
                ptcoll.Add(vptUL)

                ' draw a rectangle on a no-plot layer ("defpoints") as a guide to the users for manual panel placement
                ' set "defpoints" layer current
                Dim acLyrTbl As LayerTable
                Dim acLyrTblRec As LayerTableRecord
                ' start transaction
                Using acTrans As Transaction = myDB.TransactionManager.StartTransaction()
                    acLyrTbl = acTrans.GetObject(myDB.LayerTableId, OpenMode.ForRead)

                    acLyrTblRec = acTrans.GetObject(acLyrTbl("0"), OpenMode.ForWrite)
                    ' thaw layer
                    acLyrTblRec.IsFrozen = False
                    ' turn layer on
                    acLyrTblRec.IsOff = False
                    'make layer current
                    myDB.Clayer = acLyrTbl("0")

                    '' Open the Block table record Model space for write
                    Dim acBlkTbl As BlockTable
                    Dim acBlkTblRec As BlockTableRecord

                    acBlkTbl = acTrans.GetObject(myDB.BlockTableId, OpenMode.ForRead)
                    acBlkTblRec = acTrans.GetObject(acBlkTbl(BlockTableRecord.ModelSpace), OpenMode.ForWrite)

                    Dim whoa As Integer
                    whoa = 1
                    Do Until whoa = 50
                        ' draw the rectangle
                        Dim rect As New DatabaseServices.Polyline2d(Poly2dType.SimplePoly, ptcoll, 0, True, 0, 0, Nothing)
                        '' Add the new object to Model space and the transaction
                        acBlkTblRec.AppendEntity(rect)
                        acTrans.AddNewlyCreatedDBObject(rect, True)
                        whoa = whoa + 1
                    Loop

                    ' Commit the changes
                    acTrans.Commit()
                End Using
            End Using

 

0 Likes
Accepted solutions (1)
1,401 Views
6 Replies
Replies (6)
Message 2 of 7

Anonymous
Not applicable

It doesn't look like you save the database after you commit the changes.  Look at the SaveAs method.

0 Likes
Message 3 of 7

jshimkus
Advocate
Advocate

Thanks, when I would try that the program would throw an access error on that line so I got rid of it.

I assume because there are lock files (dwl) for the drawing database I have open.

This is what I used...

myDB.SaveAs(dbPath, DwgVersion.Current)

 

Anything else?

 

0 Likes
Message 4 of 7

jshimkus
Advocate
Advocate

I cleaned up the code and showed where I added the Savas

I get an "eFileSharingViolation" error when the program hits the myDB2.SaveAs line.

 

        dbPath = "O:\projects\Test Location\Wallframingissue10\Views\View-Partition1.dwg"
        Using myDB2 As Database = New Database(False, False)
            myDB2.ReadDwgFile(dbPath, FileOpenMode.OpenForReadAndWriteNoShare, True, "")
            Dim vptLL As Point3d = New Point3d(0, 0, 0)
            Dim vptUL As Point3d = New Point3d(vptLL.X, 12, 0)
            Dim vptUR As Point3d = New Point3d(12, vptUL.Y, 0)
            Dim vptLR As Point3d = New Point3d(vptUR.X, vptLL.Y, 0)
            Dim ptcoll As New Point3dCollection

            ptcoll.Add(vptLL)
            ptcoll.Add(vptLR)
            ptcoll.Add(vptUR)
            ptcoll.Add(vptUL)

            ' draw a rectangle on a no-plot layer ("defpoints") as a guide to the users for manual panel placement
            ' set "defpoints" layer current
            Dim acLyrTbl As LayerTable
            Dim acLyrTblRec As LayerTableRecord
            ' start transaction
            Using acTrans As Transaction = myDB2.TransactionManager.StartTransaction()
                acLyrTbl = acTrans.GetObject(myDB2.LayerTableId, OpenMode.ForRead)

                acLyrTblRec = acTrans.GetObject(acLyrTbl("0"), OpenMode.ForWrite)
                ' thaw layer
                acLyrTblRec.IsFrozen = False
                ' turn layer on
                acLyrTblRec.IsOff = False
                'make layer current
                myDB2.Clayer = acLyrTbl("0")

                '' Open the Block table record Model space for write
                Dim acBlkTbl As BlockTable
                Dim acBlkTblRec As BlockTableRecord

                acBlkTbl = acTrans.GetObject(myDB2.BlockTableId, OpenMode.ForRead)
                acBlkTblRec = acTrans.GetObject(acBlkTbl(BlockTableRecord.ModelSpace), OpenMode.ForWrite)

                ' draw the rectangle
                Dim rect As New DatabaseServices.Polyline2d(Poly2dType.SimplePoly, ptcoll, 0, True, 0, 0, Nothing)
                '' Add the new object to Model space and the transaction
                acBlkTblRec.AppendEntity(rect)
                acTrans.AddNewlyCreatedDBObject(rect, True)
                ' Commit the changes
                acTrans.Commit()
            End Using
            myDB2.SaveAs(dbPath, DwgVersion.Current)
        End Using

 

0 Likes
Message 5 of 7

Anonymous
Not applicable
Accepted solution

try 

myDB2.ReadDwgFile(dbPath, System.IO.FileShare.ReadWrite, True"")
0 Likes
Message 6 of 7

ProfWolfMan
Advocate
Advocate

 

------------------------------------------------------------------------------
'Is it even possible to add objects to a drawing without actually opening it?'
------------------------------------------------------------------------------
Hi,
Without opening the drawing you want to do something?
Then have a look about ObjectDBX in this forum.
There is lot of interesting stuff about dbx.
Regards,
G

 

Thanks & Regards,
G
0 Likes
Message 7 of 7

jshimkus
Advocate
Advocate

That got it!

Thanks Jeffrey.

0 Likes