.NET

Reply
Valued Contributor
RPeter
Posts: 80
Registered: ‎01-09-2009
Message 1 of 15 (828 Views)
Accepted Solution

Copy all objects to new drawing template

828 Views, 14 Replies
11-10-2010 06:25 AM

I've tried this one Autodesk Developer Help

But it hangs.

 

What i need to do is copy all objects into a new drawingtemplate and automaticaly run some code over this.

And close the drawing where i started the program.

Mentor
MarkPendergraft
Posts: 375
Registered: ‎06-27-2005
Message 2 of 15 (787 Views)

Re: Copy all objects to new drawing template

11-11-2010 07:29 AM in reply to: RPeter

My first inclination would be to ask if you are using the session commandflag and if you are locking the documents.  But without seeing your code, it's hard to tell.

 

-Mark

Valued Contributor
RPeter
Posts: 80
Registered: ‎01-09-2009
Message 3 of 15 (755 Views)

Re: Copy all objects to new drawing template

11-14-2010 10:28 PM in reply to: RPeter
Public Sub Copy2Template(ByVal oAppAutocadDWT As String, ByVal oAppMechanicalDWT As String, ByVal oAppCivilDWT As String)
        Dim acObjIdColl As ObjectIdCollection = New ObjectIdCollection()
        Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
        Dim acCurDb As Database = acDoc.Database
        Dim oName As String = acDoc.Name

        Dim sTemplatePath As String = oAppAutocadDWT
        Dim oCadVer As String = Left(Right(SystemObjects.DynamicLinker.ProductKey, 7), 3)
        If oCadVer = "001" Then     'Autocad
            sTemplatePath = oAppAutocadDWT
        ElseIf oCadVer = "000" Then 'Civil
            sTemplatePath = oAppCivilDWT
        ElseIf oCadVer = "005" Then 'Mechanical
            sTemplatePath = oAppMechanicalDWT
        End If

        Dim acDocMgr As DocumentCollection = Application.DocumentManager
        Dim acNewDoc As Document = acDocMgr.Add(sTemplatePath)
        acDocMgr.MdiActiveDocument = acNewDoc
        acDoc.Database.CloseInput(True)
        acDoc.CloseAndDiscard()
    End Sub

The document is locked.

When i try to close the source dwg it says "Drawing is busy" and doesn't continue the rest of the code.

I cut the code that copies the objects, (see code above) even that doesn't work...

Mentor
MarkPendergraft
Posts: 375
Registered: ‎06-27-2005
Message 4 of 15 (735 Views)

Re: Copy all objects to new drawing template

11-15-2010 08:53 AM in reply to: RPeter

Your code works fine for me.  However, I set the CommandFlags.Session variable in the sub that called it.

 

The reason it is failing on the acDoc.CloseAndDiscard() call is because you are not calling it from the session.  When you run a command (without setting the session command flag), you are running it from the drawing that you typed the command into, which means that the code is trying to continue running after you told the document that is hosting it to close.  If you set the session commandflag it will run outside the document you are working in, and you shouldn't have any problems.

The sub you are calling should look something like this:

 

<CommandMethod("testcopy", CommandFlags.Session)> Public Sub testcopy()

Valued Contributor
RPeter
Posts: 80
Registered: ‎01-09-2009
Message 5 of 15 (713 Views)

Re: Copy all objects to new drawing template

11-16-2010 04:13 AM in reply to: RPeter
    <CommandMethod("C2TMP", CommandFlags.Session)> _
    Public Sub C2TMP()
        Instellingen()
        Copy2Template(oAppAutocadDWT, oAppMechanicalDWT, oAppCivilDWT)
    End Sub

    Public Sub Copy2Template(ByVal oAppAutocadDWT As String, ByVal oAppMechanicalDWT As String, ByVal oAppCivilDWT As String)
        Dim acObjIdColl As ObjectIdCollection = New ObjectIdCollection()
        Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
        Dim acCurDb As Database = acDoc.Database
        Dim acDocEd As Editor = Application.DocumentManager.MdiActiveDocument.Editor
        Dim acDocMgr As DocumentCollection = Application.DocumentManager
        Dim acSSPrompt As PromptSelectionResult
        acSSPrompt = acDocEd.SelectAll()
        Dim acSSet As SelectionSet = acSSPrompt.Value
        Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()
            For Each oSel As SelectedObject In acSSet
                acObjIdColl.Add(oSel.ObjectId)
            Next
            acTrans.Commit()
        End Using
        Dim sTemplatePath As String = oAppAutocadDWT
        Dim oCadVer As String = Left(Right(SystemObjects.DynamicLinker.ProductKey, 7), 3)
        If oCadVer = "001" Then     'Autocad
            sTemplatePath = oAppAutocadDWT
        ElseIf oCadVer = "000" Then 'Civil
            sTemplatePath = oAppCivilDWT
        ElseIf oCadVer = "005" Then 'Mechanical
            sTemplatePath = oAppMechanicalDWT
        End If

        Dim acNewDoc As Document = acDocMgr.Add(sTemplatePath)
        Dim acDbNewDoc As Database = acNewDoc.Database
        Using acLckDoc As DocumentLock = acNewDoc.LockDocument()
            Using acTrans = acDbNewDoc.TransactionManager.StartTransaction()
                Dim acBlkTblNewDoc As BlockTable
                acBlkTblNewDoc = acTrans.GetObject(acDbNewDoc.BlockTableId, OpenMode.ForRead)
                Dim acBlkTblRecNewDoc As BlockTableRecord
                acBlkTblRecNewDoc = acTrans.GetObject(acBlkTblNewDoc(BlockTableRecord.ModelSpace), OpenMode.ForRead)
                Dim acIdMap As IdMapping = New IdMapping()
                acCurDb.WblockCloneObjects(acObjIdColl, acBlkTblRecNewDoc.ObjectId, acIdMap, DuplicateRecordCloning.Ignore, False)
                acTrans.Commit()
            End Using
        End Using
        acDoc.CloseAndDiscard()
        acDocMgr.MdiActiveDocument = acNewDoc
    End Sub

 Insetllingen gets some parameters from an xml file (this works)

Then it calls to Copy2Template

Everything goes great untill the part acdoc.closeanddiscard() i get the error Drawing is busy!

 

I've added the CommandFlags.Session to the command as you suggested.

After the Copy2Template i need to run some more code (wich i already have but not included in this command) in the new document

Mentor
MarkPendergraft
Posts: 375
Registered: ‎06-27-2005
Message 6 of 15 (705 Views)

Re: Copy all objects to new drawing template

11-16-2010 08:22 AM in reply to: RPeter

Your code runs without error for me.  Seeing as Instellingen() runs prior to the code that is crashing, I would try commenting it out and seeing if that makes a difference.  Perhaps you are tying something up in the drawing, making it unable to close.  I would also look at your templates.  Does it produce the same error with all the templates?  Do the templates all have the same objects in them?

 

-Mark P.

Valued Contributor
RPeter
Posts: 80
Registered: ‎01-09-2009
Message 7 of 15 (683 Views)

Re: Copy all objects to new drawing template

11-16-2010 10:24 PM in reply to: RPeter

The Instellingen() sub did not make it crash.

What i've did to solve:

 

I've removed the close drawing in the copy2 template and added it to the commandsub like this:

Instellingen()
Copy2Template(oAppAutocadDWT, oAppMechanicalDWT, oAppCivilDWT)
Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
acDoc.Database.CloseInput(True)
acDoc.CloseAndDiscard()

 

After this modification it worked without any errors.

I've added my other procedures to the command and they al work great now.

 

So problem solved.

 

Regards

 

Peter

Contributor
jamkhp
Posts: 24
Registered: ‎05-19-2011
Message 8 of 15 (255 Views)

Re: Copy all objects to new drawing template

03-04-2013 02:45 AM in reply to: RPeter

I ma doing the same but one change is I want only selected entities to move to a new drawing.

and I am using this code, but it is not properly working for me :smileysad: :smileysad :-(

here is the code.

Public Shared Sub SelectObjectsOnscreen()
Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
Dim acCurDb As Database = acDoc.Database
Dim acObjIdColl As ObjectIdCollection = New ObjectIdCollection()
Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()
Using mydoclock As DocumentLock = acDoc.LockDocument()
Dim acSSPrompt As PromptSelectionResult = acDoc.Editor.GetSelection()
If acSSPrompt.Status = PromptStatus.OK Then
Dim acSSet As SelectionSet = acSSPrompt.Value
For Each acSSObj As SelectedObject In acSSet
If Not IsDBNull(acSSObj) Then
Dim myObjID As ObjectId = acSSObj.ObjectId
acObjIdColl.Add(myObjID)
End If
Next
acSSet.Dispose()
End If
End Using
End Using
acDoc.Dispose()
Dim strTemplatePath As String = "BMGTemplate.dwt"
Dim acDocMgr As DocumentCollection = Application.DocumentManager
Dim acDoc1 As Document = acDocMgr.Add(strTemplatePath)
Dim acCurDb1 As Database = acDoc1.Database
acDocMgr.MdiActiveDocument = acDoc1 ' after this line, the Autocad is setting focus to old drawing from which I copied.
Dim ed As Editor = acDoc1.Editor
Using mydoclock As DocumentLock = acDoc1.LockDocument()
Using acTrans1 As Transaction = acCurDb1.TransactionManager.StartTransaction
Dim acBlk As BlockTable = acCurDb1.BlockTableId.GetObject(OpenMode.ForRead)
Dim acBlkrec As BlockTableRecord = acBlk(BlockTableRecord.ModelSpace).GetObject(OpenMode.ForRead)
Dim acIdMap As IdMapping = New IdMapping()
acCurDb1.WblockCloneObjects(acObjIdColl, acBlkrec.Id, acIdMap, DuplicateRecordCloning.Replace, False)
acTrans1.Commit()
Application.DocumentManager.MdiActiveDocument.Editor.UpdateScreen()
Application.DocumentManager.MdiActiveDocument.Editor.Regen()
End Using
End Using
ed.Document.SendStringToExecute("._zoom _all ", True, False, False)
Dim myname As String = selectTextToName() ' this sub selects the a text in the new drawing.
Dim di As DirectoryInfo = New DirectoryInfo("C:\myToolingTDMs")
If Not di.Exists Then
di.Create()
End If
Dim mybasefileLoc As String = di.ToString + "\" + myname + "_TDM.dwg"
acDoc1.CloseAndSave(mybasefileLoc)
End Sub

 

 

Please help.

 

Valued Contributor
RPeter
Posts: 80
Registered: ‎01-09-2009
Message 9 of 15 (247 Views)

Re: Copy all objects to new drawing template

03-04-2013 05:45 AM in reply to: jamkhp

I modified my code (fast tested on Acad 2013)

Public Sub Copy2Template()
        Dim MyTemplate As String = "E:\Autocad.dwt"

        Dim oic As ObjectIdCollection = New ObjectIdCollection()

        Dim doc As Document = Application.DocumentManager.MdiActiveDocument
        Dim ed As Editor = doc.Editor
        Dim db As Database = doc.Database
        Dim dm As DocumentCollection = Application.DocumentManager

        Dim MySelection As PromptSelectionResult = ed.GetSelection()

        If MySelection.Status = PromptStatus.OK Then
            If MySelection.Value.Count > 0 Then
                Dim ndoc As Document = DocumentCollectionExtension.Add(dm, MyTemplate)
                Dim ndb As Database = ndoc.Database

                Using dl As DocumentLock = doc.LockDocument()
                    Using tr As Transaction = db.TransactionManager.StartTransaction()
                        For Each so As SelectedObject In MySelection.Value
                            oic.Add(so.ObjectId)
                        Next
                        tr.Commit()
                    End Using
                End Using

                Using ndl As DocumentLock = ndoc.LockDocument()
                    Using ntr = ndb.TransactionManager.StartTransaction()
                        Dim nbt As BlockTable = ntr.GetObject(ndb.BlockTableId, OpenMode.ForRead)
                        Dim nbtr As BlockTableRecord = ntr.GetObject(nbt(BlockTableRecord.ModelSpace), OpenMode.ForRead)
                        Try
                            db.WblockCloneObjects(oic, nbtr.ObjectId, New IdMapping(), DuplicateRecordCloning.Ignore, False)
                        Catch ex As Exception
                        End Try
                        ntr.Commit()
                    End Using
                End Using
            End If
        End If
    End Sub

 Regards

 

Peter

Contributor
jamkhp
Posts: 24
Registered: ‎05-19-2011
Message 10 of 15 (240 Views)

Re: Copy all objects to new drawing template

03-04-2013 07:29 AM in reply to: RPeter

Hi Peter, thanks for your code,

However, I cant figure out this line

Dim ndoc As Document = DocumentCollectionExtension.Add(dm, MyTemplate)

 

"DocumentCollectionExtension" , that one is not a function, I am using Acad 2011, is that something new added in the .net API of Acad 13, or it is a COM object ?

 

You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.

Need installation help?

Start with some of our most frequented solutions to get help installing your software.

Ask the Community