I am trying to open a template in AutoCad 2014. I start AutoCad and immediately have Drawing1.dwg available to save; this is fine. I click a button which executes an "OpenTemplate" command. This opens Drawing2 with the correct template, but has some weird behavior. Here's the code I am using:
<CommandMethod("OpenTemplate")> _ Public Sub OpenTemplate() Dim acDocMgr As DocumentCollection = Application.DocumentManager Dim acDoc As Document Dim ed As Editor Dim db As Database 'Opens template... Visually still see Drawing1, but I can see Drawing2 is opened acDoc = DocumentCollectionExtension.Add(acDocMgr, "C:\CustomTemplate.dwt")
'Flashes a bit and it visually sets the user to view Drawing2 acDocMgr.MdiActiveDocument = acDoc MsgBox("The above code is executed and focus is sent to Drawing2." & vbNewLine & _ "Until I go back to Drawing1, this MsgBox will not be hit. But I will be able " & _
"to control the Acad Environment in Drawing2 (buttons etc)") ed = acDoc.Editor db = acDoc.Database End Sub
I am wondering how do I open a template correctly? I am obviously doing something wrong unless we just can't do it this way. I find it weird that I must go back to Drawing1 in order to keep the ball rolling in my code. If I stay in Drawing2 (which is what I want) nothing happens, it waits until I go back to Drawing1.
Hopefully this makes sense ...
Thank you!
Alex
Solved! Go to Solution.
Solved by Keith.Brown. Go to Solution.
Solved by norman.yuan. Go to Solution.
Solved by Keith.Brown. Go to Solution.
I have been using this code for awhile and it works perfectly for me.
Private Shared Sub CreateDrawing(fileName As String) Dim acDocMgr As DocumentCollection = Application.DocumentManager Dim acDoc As Document = acDocMgr.Add(fileName) acDocMgr.MdiActiveDocument = acDoc End Sub
The only difference i see is that you are adding the document to the DocumentCollectionExtension and not the DocumentCollection.
Depending the purpose of opening a drawing (existing, or a new), you want to consider use CommandFlags.Session flag in the CommandMethod attribute method.
In your case where you did not use Session flag, so, if a drawing is added.opened, AutoCAD opens it on top of existing drawing (current MdiActiveDocument) visually, and then since it is not the MdiActiveDocument, it then falls behind, thus the visual flickering you noticed.
If your purpose of open a drawing is to do something with code only (and likely close it when done), then this is OK, except the flickering is a bit annoying.
If the command method has Session flag, the newly added/opened drawing automatically becomes MdiActiveDocument. If you want to open drawing and leave open for user to work with, you'd better set Session flag, so that the opened drawing automatically becomes MdiActiveDocument and opned as forefront drawing in AutoCAd editor.
Norman Yuan
Good Point Norman,
Here is my command that I use to call the function posted above. I have the CommandFlag.Sessions as an attribute.
[CommandMethod("NewFromTemplateMEPUSImperial", CommandFlags.Session)] public static void CommandNewFromTemplateMep() { CreateDrawing("Aecb Model (US Imperial CTB).dwt"); }
I got lazy and didnt convert this to VB.
Hi Keith, thanks for the reply.
For some reason, the DocumentCollectionExtension is the only one that works for me, see below:
I saw that code snippet while doing some research but went on to something else when I saw it didn't work... Any ideas why it won't let me add?
I believe that is because I used Autodesk.AutoCAD.ApplicationServices.Core.Application instead of Autodesk.AutoCAD.ApplicationServices.Application.
In other words i placed this at the top
Snippet
using Application = Autodesk.AutoCAD.ApplicationServices.Core.Application;
again in c#