CreateObject Revisited

Discussion in 'AutoCAD' started by Ken Hutson, Apr 22, 2004.

  1. Ken Hutson

    Ken Hutson Guest

    Hi Group,

    From VB6, I create an instance of AutoCAD with CreateObject. When I issue
    the insert command at the AutoCAD command prompt and browse, my starting
    location in the Select Drawing File dialog is set to C:\winnt\system32\.
    Why is this and how can I change this to begin looking in a folder of my
    choice?

    Regards,
    Ken Hutson
    San Antonio, Texas
     
    Ken Hutson, Apr 22, 2004
    #1
  2. Maybe I'm missing something with your post...why are you using the command
    prompt to insert something? If you started the session in code, use code to
    do the insert and you won't have a problem.
     
    Mike Tuersley, Apr 22, 2004
    #2
  3. Ken Hutson

    Ed Jobe Guest

    Besides looking for ways to supply the path without having to browse as Mike
    noted, this is a good question. Think back to dos days and how the dos
    prompt displayed the current path. In windows this is the working dir. You
    can use the CurDir function to return this path, which is set in the 'Start
    In' field of a shortcut. But since you didn't start acad from a shortcut,
    apparently windows is using the system dir. You can use the ChDrive and
    ChDir functions to change the working dir.
     
    Ed Jobe, Apr 22, 2004
    #3
  4. Ken Hutson

    Ken Hutson Guest

    For Mike,
    I can try and clarify. Usually, we start AutoCAD through the shortcut where
    we can specify the "start in" location as Ed points out. For us, we tap a
    %username% enviroment variable. However, assume that user starts VB App.
    VB App can't detect a running instance of AutoCAD but it needs AutoCAD
    running so it can operate. VB App invokes CreateObject. AutoCAD starts and
    VB App does it's thing. Maybe it inserts a block or whatever. So the user
    finishes using VB App and continues the editing session in the drawing
    AutoCAD started when CreateObject started AutoCAD. User types "insert" at
    the command prompt and the file selection dialog appears. User finds an
    unfamiliar and sensitive area which they would normally have no business in
    anyway. "Save" command exibits the same behavior. My goal is to start
    them in the directory they are used to seeing.

    For Ed,
    That is the FileSystemObject. Correct? I'll give it a whirl. Also am
    investigating ShellExecute function call. Looks like it takes parameters.

    Cheers,
    Ken Hutson
    San Antonio, TX
     
    Ken Hutson, Apr 22, 2004
    #4
  5. Ken Hutson

    Ed Jobe Guest

    Nope, plain old vb functions, as basic as their dos counterparts. Just set
    the working dir before you start acad. Also, you wouldn't have this problem
    if the REMEMBERFOLDERS sysvar was set to 1.
     
    Ed Jobe, Apr 22, 2004
    #5
  6. Ken Hutson

    Ken Hutson Guest

    Ed,
    Sorry I didn't specify. We are STILL on R2000. REMEMBERFOLDERS must have
    been added later.

    Thanks for the assist,
    Ken Hutson
    San Antonio, TX
     
    Ken Hutson, Apr 22, 2004
    #6
  7. I'm not following the gist of this thread all that well, so forgive me if my
    comment is off-base. But it sounds like you don't like where the various VB
    apps are leaving your working directory after they do their stuff. I would
    think that the place to tackle this problem is in the VB apps themselves.
    Could you modify them to store the current working dir when they start
    running, and then restore it when they're done (either normal or error
    exits)?

    HTH,
    James
     
    James Belshan, Apr 23, 2004
    #7
  8. Ken Hutson

    Ken Hutson Guest

    James,
    Let's say AutoCAD user starts VB app because he knows that there is some
    function in VB App which will expedite an AutoCAD task. User presses a
    button in VB App to execute the desired function. Function may need an
    instance of AutoCAD so it checks to see if AutoCAD is running. Suppose it
    sees AutoCAD is NOT running. Maybe user forgot to start AutoCAD. So VB App
    calls CreateObject(, "AutoCAD.Application"). AutoCAD starts and VB App
    function does whatever it was designed to do in AutoCAD.

    Further, let's say user continues to edit the default drawing that was
    created when CreateObject instantiated the AutoCAD process. For example,
    user types "Save", "Insert" or "XREF attach". File dialog box comes up. In
    this case, on my system, the default path in the file dialog box comes up
    C:\winnt\system32\. I don't want user in this particular folder. They have
    no business there. Plus more than likely, it's a long traverse to find the
    path that is of interest to them.

    VB App didn't change a path at all. Problem is that CreateObject provides
    no way to specify a start up folder for AutoCAD (like a shortcut can) and I
    don't see a property on the acaddocument or acadapplication to change it
    after AutoCAD starts. Now the acaddocument has a Path property, but it's
    read only. Am I missing something?

    Ed,
    By the way, I tried Chdir function both before and after CreateObject. No
    effect. An API call to ShellExecute works but it's asynchronous.

    Cheers,
    Ken Hutson
    San Antonio, TX
     
    Ken Hutson, Apr 23, 2004
    #8
  9. Here's an ugly kludge that at least leaves them in the Windows Temp
    directory, a little less dangerous place.... and actually the TempFile API
    will take any directory as an argument, so you could leave them anywhere you
    want -- but the Win Temp directory is pretty guaranteed to exist, be
    writeable, etc....least danger of runtime errors.

    Option Explicit

    ' courtesy Randy Birch @
    http://vbnet.mvps.org/index.html?code/fileapi/gettempfilename.htm
    ' helpful comments removed, code adulterated for my purposes

    Private Declare Function GetTempPath _
    Lib "kernel32" Alias "GetTempPathA" _
    (ByVal nBufferLength As Long, _
    ByVal lpBuffer As String) As Long

    Private Declare Function GetTempFileName _
    Lib "kernel32" Alias "GetTempFileNameA" _
    (ByVal lpszPath As String, _
    ByVal lpPrefixString As String, _
    ByVal wUnique As Long, _
    ByVal lpTempFileName As String) As Long

    Private Const MAX_PATH As Long = 260

    Public Sub CommandButton1_Click()

    Dim TempXLfilename As String
    Dim AcadApp As Object
    Set AcadApp = CreateObject("AutoCAD.Application")
    TempXLfilename = GetTempXLfilename
    AcadApp.activedocument.SaveAs TempXLfilename
    AcadApp.activedocument.Close False 'could leave file open if you want to

    'Kill TempXLfilename
    'trying to clean up temp file is causing error... dunno why...
    End Sub

    Private Function GetTempXLfilename()
    Dim tempfilename As String
    tempfilename = CreateTempFilename
    Name tempfilename As tempfilename & ".xls"
    tempfilename = tempfilename & ".xls"
    Kill tempfilename
    GetTempXLfilename = tempfilename
    End Function

    Private Function GetSystemTempPath() As String
    Dim result As Long
    Dim buff As String
    buff = Space$(MAX_PATH)
    result = GetTempPath(MAX_PATH, buff)
    GetSystemTempPath = Left$(buff, result)
    End Function


    Private Function CreateTempFilename() As String

    Dim result As Long
    Dim buff As String

    buff = Space$(MAX_PATH)
    result = GetTempFileName(GetSystemTempPath(), "vbn", 0, buff)
    If result <> 0 Then
    CreateTempFilename = Left$(buff, InStr(buff, Chr$(0)) - 1)
    End If
    End Function


    Good luck,
    James
     
    James Belshan, Apr 23, 2004
    #9
  10. Okay, I follow now. Have you tried opening AutoCAD and applying a profile?
    That might get your paths back....
     
    Mike Tuersley, Apr 23, 2004
    #10
  11. Ken Hutson

    Ken Hutson Guest

    Mike,
    Just tried that. No effect. Good try though. I have two profiles.
    Default and KJH (my initials). Funny thing is, my profile is already active
    after CreateObject call. Seems like I need to get access to AutoCAD File
    Selection object. I am thinking it may be using information from a Windows
    component.

    Thanks,
    Ken Hutson
    San Antonio, TX
     
    Ken Hutson, Apr 23, 2004
    #11
  12. Ken Hutson

    Ken Hutson Guest

    Anyone from AutoDesk care to comment on this thread?
     
    Ken Hutson, Apr 23, 2004
    #12
  13. Oops... hehe.. I must've been tired. The two instances of ".xls" in the
    code I wrote needs to be changed to ".dwg". Otherwise the file gets saved
    to a different name, *.xls.dwg
    James
     
    James Belshan, Apr 23, 2004
    #13
  14. Ken Hutson

    Ed Jobe Guest

    I was able to duplicate your situation by setting REMEMBERFOLDERS to 0.
    Earlier when I suggested to set the working dir, I neglected to mention that
    each app has its own working dir. You need to set it from withing acad, not
    your vb app. I created a function that did this and called it from the vb
    app, still the file dialog opens in system32. You can try this from the acad
    command line:
    "vbastmt ChDir sompath"
    Then open the file dialog and it still opens in system32. I looked in the
    registry and didn't find any place that was storing this path.
     
    Ed Jobe, Apr 23, 2004
    #14
  15. Ken Hutson

    Ed Jobe Guest

    My memory finally kicked in...I did some work a while ago on this. I was
    able to set the registy path for the Insert dialog (I have it in lisp so you
    can just add it to a button macro) so that you could route the folder to
    your block lib's main folder, but for some reason you can set the reg keys
    for the Open/Save dialog just fine, but the dialog doesn't recognize the
    change.
     
    Ed Jobe, Apr 26, 2004
    #15
  16. Ken Hutson

    Ken Hutson Guest

    Thanks to all for your exploration,

    The Excel Application has a property called DefaultFilePath. It's
    read/write. I'm on AutoCAD 2000 and I don't see anything similar. Don't
    know about more recent AutoCAD releases but perhaps since AutoDesk is
    aligning itself with Microsoft, it wouldn't be a bad idea to add this
    property to the AutoCAD Application object.

    As it is, I have resigned myself to design my App so that it will advise the
    user to start AutoCAD if it does not detect a running instance of AutoCAD.

    Cheers,
    Ken Hutson
    San Attonio, TX
     
    Ken Hutson, Apr 26, 2004
    #16
  17. Have a look at some of the registry keys in this location
    HKEY_CURRENT_USER\Software\Autodesk\AutoCAD\R16.0\ACAD-202:409\Profiles\<<Unnamed Profile>>\Dialogs
    I think they will help but maybe not for the save dialog.
    Regards - Nathan
     
    Nathan Taylor, Apr 27, 2004
    #17
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.