.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Opening a template issue

7 REPLIES 7
SOLVED
Reply
Message 1 of 8
Alex_Crook
656 Views, 7 Replies

Opening a template issue

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

7 REPLIES 7
Message 2 of 8
Alex_Crook
in reply to: Alex_Crook

I forgot some important information. The following code will work properly when I use a standard version (VANILLA?) but not when I use the Mechanical version.

If I use the Mechanical version, I get the template issue described in the first post.
Message 3 of 8
Keith.Brown
in reply to: Alex_Crook

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.

Message 4 of 8
norman.yuan
in reply to: Alex_Crook

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

Drive CAD With Code

EESignature

Message 5 of 8
Keith.Brown
in reply to: 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.

Message 6 of 8
Alex_Crook
in reply to: Keith.Brown

Hi Keith, thanks for the reply.

 

For some reason, the DocumentCollectionExtension is the only one that works for me, see below:

 

docMgrAdd.png

 

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?

Message 7 of 8
Keith.Brown
in reply to: Alex_Crook

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#

Message 8 of 8
Alex_Crook
in reply to: Keith.Brown

Excellent! Thank you everyone, it fixed it!

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost