Author Topic: launch command from exe not loaded in cad  (Read 6949 times)

0 Members and 1 Guest are viewing this topic.

nobody

  • Swamp Rat
  • Posts: 861
  • .net stuff
launch command from exe not loaded in cad
« on: March 02, 2013, 09:08:06 AM »
Is that possible? I created a form that contains some tools I use, and buttons for links to websites I visit all the time. I open that before I open Autocad session.  Can I make it launch a command in autocad?  Basically I get tired of loading my autolisp routines (don't make it automatic in case someone else at company hops on computer). 
« Last Edit: March 03, 2013, 12:49:49 AM by Alien »

nobody

  • Swamp Rat
  • Posts: 861
  • .net stuff
Re: launch command from exe not loaded in cad
« Reply #1 on: March 04, 2013, 09:09:09 PM »
Stumped the pros :D

snownut2

  • Swamp Rat
  • Posts: 971
  • Bricscad 22 Ultimate
Re: launch command from exe not loaded in cad
« Reply #2 on: March 04, 2013, 09:18:36 PM »
You can probably get it to run a batch file to rename a file acaddoc to autoload when acad starts ....

BlackBox

  • King Gator
  • Posts: 3770
Re: launch command from exe not loaded in cad
« Reply #3 on: March 05, 2013, 08:51:05 AM »
Is that possible?

Yes.

I created a form that contains some tools I use, and buttons for links to websites I visit all the time. I open that before I open Autocad session. 

User preference.

You could instead open AutoCAD first, and have it launch your 'form' at startup, as one alternative. Another, would be to host a WPF User Control within an AutoCAD Tool Palette, and never have to 'leave' AutoCAD.

Can I make it launch a command in autocad? 

Yes.

Basically I get tired of loading my autolisp routines (don't make it automatic in case someone else at company hops on computer).

Methinks, you're doing it wrong (if it's that tedious)... Consider the Autoload function, or the Autoloader mechanism (for 2012+).
"How we think determines what we do, and what we do determines what we get."

nobody

  • Swamp Rat
  • Posts: 861
  • .net stuff
Re: launch command from exe not loaded in cad
« Reply #4 on: March 05, 2013, 03:11:11 PM »
I think i need to try and clarify what I'm trying to figure out. Everyone with C# for CAD is familiar with creating .exe / dll files that include all the cad references to work in autocad by using netload. I know how to do this.

I am wondering if it's possible to "inject" a command into autocad, externally through a c# program not "netloaded" in cad.  Instead of loading everything through cad, can  C# find and locate the ACAD process in the system's running processes, and simply inject itself into the software. 

A matter of curiosity really.  In a way it's a hack I suppose. 

Is that possible?

Yes.

I created a form that contains some tools I use, and buttons for links to websites I visit all the time. I open that before I open Autocad session. 

User preference.

You could instead open AutoCAD first, and have it launch your 'form' at startup, as one alternative. Another, would be to host a WPF User Control within an AutoCAD Tool Palette, and never have to 'leave' AutoCAD.

Can I make it launch a command in autocad? 

Yes.

Basically I get tired of loading my autolisp routines (don't make it automatic in case someone else at company hops on computer).

Methinks, you're doing it wrong (if it's that tedious)... Consider the Autoload function, or the Autoloader mechanism (for 2012+).

BlackBox

  • King Gator
  • Posts: 3770
Re: launch command from exe not loaded in cad
« Reply #5 on: March 05, 2013, 03:19:32 PM »
I think i need to try and clarify what I'm trying to figure out. Everyone with C# for CAD is familiar with creating .exe / dll files that include all the cad references to work in autocad by using netload. I know how to do this.

I am wondering if it's possible to "inject" a command into autocad, externally through a c# program not "netloaded" in cad.  Instead of loading everything through cad, can  C# find and locate the ACAD process in the system's running processes, and simply inject itself into the software. 

A matter of curiosity really.  In a way it's a hack I suppose. 

I understood your request just fine, this is not at all uncommon... Yes, it can be done.

More specifically, you need to (rather should) check for an active process, prior to creating a new instance... Consider the GetProcessByName() Method as a starting point.

"How we think determines what we do, and what we do determines what we get."

nobody

  • Swamp Rat
  • Posts: 861
  • .net stuff
Re: launch command from exe not loaded in cad
« Reply #6 on: March 05, 2013, 10:23:11 PM »
Very cool! I'll look more into it! Thanks blackbox!

I think i need to try and clarify what I'm trying to figure out. Everyone with C# for CAD is familiar with creating .exe / dll files that include all the cad references to work in autocad by using netload. I know how to do this.

I am wondering if it's possible to "inject" a command into autocad, externally through a c# program not "netloaded" in cad.  Instead of loading everything through cad, can  C# find and locate the ACAD process in the system's running processes, and simply inject itself into the software. 

A matter of curiosity really.  In a way it's a hack I suppose. 

I understood your request just fine, this is not at all uncommon... Yes, it can be done.

More specifically, you need to (rather should) check for an active process, prior to creating a new instance... Consider the GetProcessByName() Method as a starting point.

BlackBox

  • King Gator
  • Posts: 3770
Re: launch command from exe not loaded in cad
« Reply #7 on: March 05, 2013, 10:39:50 PM »
Very cool! I'll look more into it! Thanks blackbox!

You're welcome, Alien; I'm happy to help.  :-)
"How we think determines what we do, and what we do determines what we get."

TheMaster

  • Guest
Re: launch command from exe not loaded in cad
« Reply #8 on: March 06, 2013, 08:32:39 PM »

More specifically, you need to (rather should) check for an active process, prior to creating a new instance... Consider the GetProcessByName() Method as a starting point.

Perhaps you can tell us a bit more about how code running in another process injects intself into a running AutoCAD process?

Also, are you familar with COM/ActiveX and controlling AutoCAD from other processes using it?

BlackBox

  • King Gator
  • Posts: 3770
Re: launch command from exe not loaded in cad
« Reply #9 on: March 06, 2013, 09:34:45 PM »
Firstly, if I have misspoken then I'd appreciate being educated, so that if/when I again help others Tony, that I may do so with the correct information. Admittedly, I know very little of multi-threading, but enough to know that AutoCAD is not thread safe.

That said, going from memory is where I based my earlier post, from this old thread which uses a stand alone exe:


Code: [Select]
Imports AutoCAD
Imports AutoCAD.AcadDocumentClass

Module Module1
    Sub Main()
        Dim vAcadApp, vNewFile As Object
        Dim strNovellPath As String = "\\my_novell_path\dwg\"
        Dim strFileName As String = "drawing templates(xx).dwg"

        On Error Resume Next
        vAcadApp = GetObject(, "autocad.Application")
        If Err.Number <> 0 Then
            Err.Clear()
            vAcadApp = CreateObject("autocad.Application")
        End If

        vAcadApp.Visible = True
        Err.Clear()
        vNewFile = vAcadApp.Documents.Open(strNovellPath & strFileName, False)

    End Sub

End Module

This will open the existing drawing I'm wanting and if AutoCAD is already running it uses that instance of it, even if there are other drawings open in that instance, this one will be come the Active drawing. If AutoCAD is not running it will start it up and then open the requested drawing. I would appreciate anyone helping me to add the correct error handling in this. I'm still new to a lot of this .NET format.

Doesn't the Open() Method need to be invoked upon the Document Collection Object, and not the Application Object?

I think you're right on that...



Again, any clarification would be greatly appreciated, Tony.
« Last Edit: March 06, 2013, 09:47:34 PM by BlackBox »
"How we think determines what we do, and what we do determines what we get."

BlackBox

  • King Gator
  • Posts: 3770
Re: launch command from exe not loaded in cad
« Reply #10 on: March 06, 2013, 09:53:33 PM »
That said, going from memory is where I based my earlier post, from this old thread which uses a stand alone exe:

Going back further still, it appears that discussion was started +/-8 months earlier in this thread.
"How we think determines what we do, and what we do determines what we get."

TheMaster

  • Guest
Re: launch command from exe not loaded in cad
« Reply #11 on: March 07, 2013, 11:21:09 PM »
Firstly, if I have misspoken then I'd appreciate being educated, so that if/when I again help others Tony, that I may do so with the correct information. Admittedly, I know very little of multi-threading, but enough to know that AutoCAD is not thread safe.

That said, going from memory is where I based my earlier post, from this old thread which uses a stand alone exe:


Code: [Select]
Imports AutoCAD
Imports AutoCAD.AcadDocumentClass

Module Module1
    Sub Main()
        Dim vAcadApp, vNewFile As Object
        Dim strNovellPath As String = "\\my_novell_path\dwg\"
        Dim strFileName As String = "drawing templates(xx).dwg"

        On Error Resume Next
        vAcadApp = GetObject(, "autocad.Application")
        If Err.Number <> 0 Then
            Err.Clear()
            vAcadApp = CreateObject("autocad.Application")
        End If

        vAcadApp.Visible = True
        Err.Clear()
        vNewFile = vAcadApp.Documents.Open(strNovellPath & strFileName, False)

    End Sub

End Module

This will open the existing drawing I'm wanting and if AutoCAD is already running it uses that instance of it, even if there are other drawings open in that instance, this one will be come the Active drawing. If AutoCAD is not running it will start it up and then open the requested drawing. I would appreciate anyone helping me to add the correct error handling in this. I'm still new to a lot of this .NET format.

Doesn't the Open() Method need to be invoked upon the Document Collection Object, and not the Application Object?

I think you're right on that...



Again, any clarification would be greatly appreciated, Tony.

My question was regarding 'inject'(ing) into another process. It's possible for a DLL to do that, but as far as I know, an .EXE cannot inject itself into another .EXE.  Perhaps, your idea of the OP's use of 'inject' was not that, but something else.


BlackBox

  • King Gator
  • Posts: 3770
Re: launch command from exe not loaded in cad
« Reply #12 on: March 08, 2013, 12:46:01 AM »
My question was regarding 'inject'(ing) into another process. It's possible for a DLL to do that, but as far as I know, an .EXE cannot inject itself into another .EXE.  Perhaps, your idea of the OP's use of 'inject' was not that, but something else.

Fair enough... Perhaps I should have identified/corrected that use of term in Alien's (aka BC's) post. Instead, I chose to ignore the literal meaning of the term inject in terms of threads & processes, in order to infer the term invoke in it's place. Hence the response in confidence that one could enact a Command within an AutoCAD process via (external) stand alone EXE, as described in the supplementary posts from many moons ago.

Admittedly, that substitution was a personal liberty, taken based on the limited knowledge I have of both .NET development in general, and my understanding of BC's .NET background as it relates to his original request in the OP. A context that most certainly was not made clear by the brief response(s) made by me.

As always, Tony, thanks for seeking to clarify another topic; I learn something each time.

Cheers


Is that possible? I created a form that contains some tools I use, and buttons for links to websites I visit all the time. I open that before I open Autocad session.  Can I make it launch a command in autocad?  Basically I get tired of loading my autolisp routines (don't make it automatic in case someone else at company hops on computer).

"How we think determines what we do, and what we do determines what we get."

nobody

  • Swamp Rat
  • Posts: 861
  • .net stuff
Re: launch command from exe not loaded in cad
« Reply #13 on: March 08, 2013, 06:40:36 AM »
Quote

As always, Tony, thanks for seeking to clarify another topic; I learn something each time.


I'll second this; and am learning so much from you both.  Thank you.

WILL HATCH

  • Bull Frog
  • Posts: 450
Re: launch command from exe not loaded in cad
« Reply #14 on: March 08, 2013, 01:59:10 PM »
Looking at this issue from another direction...

I know that when CadWORX is being loaded up with AutoCAD it's done via command line arguments which are embedded in the shortcut that launches.  Is this just an argument that gets passed to the commandline?  If that were the case then it would be easy for you to do some custom initialization as you wish.  But sadly I don't know much on that topic.

BlackBox

  • King Gator
  • Posts: 3770
Re: launch command from exe not loaded in cad
« Reply #15 on: March 08, 2013, 02:42:16 PM »
I know that when CadWORX is being loaded up with AutoCAD it's done via command line arguments which are embedded in the shortcut that launches.  Is this just an argument that gets passed to the commandline?

I am not familiar with CadWORX product, but if it is being initialized via application icon, then methinks their using a Startup Switch to load (/ld) their plug-in(s)... Which is not to be confused with a stand alone EXE which is an entirely different Process altogether (the System.Diagnostics Class, not an actual procedure).
"How we think determines what we do, and what we do determines what we get."

WILL HATCH

  • Bull Frog
  • Posts: 450
Re: launch command from exe not loaded in cad
« Reply #16 on: March 08, 2013, 06:15:42 PM »
Thanks for pointing to that info!

The CadWORX shortcut has the following:

Target: "C:\Program Files\AutoCAD 2010\acad.exe" /P CADWorx_Plant_2010

Start in: "C:\CADWorx 2010\Plant"


From looking at the link you supplied our extraterrestrial friend could load his lsp files with a script that he calls with "/b"?

BlackBox

  • King Gator
  • Posts: 3770
Re: launch command from exe not loaded in cad
« Reply #17 on: March 08, 2013, 06:21:54 PM »
A script call could be used, yes... But it loads after Acad.lsp and AcadDoc.lsp at the end of the startup sequence, and it's not exactly the most maintainable method, never mind capable. Consider post #3 of this thread.
"How we think determines what we do, and what we do determines what we get."

WILL HATCH

  • Bull Frog
  • Posts: 450
Re: launch command from exe not loaded in cad
« Reply #18 on: March 08, 2013, 11:26:12 PM »
I was just keeping in mind that he doesn't want this to be automatically loaded in case someone else jumps on the machine.

Since I don't know defun from da fun in the lisp world... What is so significant about when Acad.lsp and AcadDoc.lsp load?  Thanks

nobody

  • Swamp Rat
  • Posts: 861
  • .net stuff
Re: launch command from exe not loaded in cad
« Reply #19 on: March 09, 2013, 10:04:10 PM »
Thanks a mill gents... this is what i ended up using.
Code: [Select]
        private void button1_Click(object sender, EventArgs e)
        {
           
            const string progID = "AutoCAD.Application";
            AcadApplication acApp = null;

            try
            {
            acApp =(AcadApplication)Marshal.GetActiveObject(progID);}
            catch
            {
                try
                {
                    Type acType =Type.GetTypeFromProgID(progID);
                    acApp = (AcadApplication)Activator.CreateInstance(acType,true);
                }
               catch
                {
                   MessageBox.Show("Cannot create object of type \"" + progID + "\"");
                 }
             }

            if (acApp != null)
            {
                acApp.Visible = true;
                acApp.ActiveDocument.SendCommand("(load \"nameof.lsp\")" + "\n");
            }
        }

BlackBox

  • King Gator
  • Posts: 3770
Re: launch command from exe not loaded in cad
« Reply #20 on: March 10, 2013, 05:01:55 AM »
I was just keeping in mind that he doesn't want this to be automatically loaded in case someone else jumps on the machine.

Since I don't know defun from da fun in the lisp world... What is so significant about when Acad.lsp and AcadDoc.lsp load?  Thanks

If the LISP was loaded by the Script, and the Script were being loaded from the /b switch In the application icon's Target Property, then anyone who uses that application icon gains access to said LISP.

As for Acad.lsp, and AcadDoc.lsp, these are user-defined files which are automatically loaded at startup, and drawing open respectively (by default) if found within the SFSP.
"How we think determines what we do, and what we do determines what we get."

nobody

  • Swamp Rat
  • Posts: 861
  • .net stuff
Re: launch command from exe not loaded in cad
« Reply #21 on: March 10, 2013, 07:28:27 AM »
I actually am less concerned about access to the lisp than the fact that I've changed the alias for almost every basic command lol... they'd be screwed hehe

I was just keeping in mind that he doesn't want this to be automatically loaded in case someone else jumps on the machine.

Since I don't know defun from da fun in the lisp world... What is so significant about when Acad.lsp and AcadDoc.lsp load?  Thanks

If the LISP was loaded by the Script, and the Script were being loaded from the /b switch In the application icon's Target Property, then anyone who uses that application icon gains access to said LISP.

As for Acad.lsp, and AcadDoc.lsp, these are user-defined files which are automatically loaded at startup, and drawing open respectively (by default) if found within the SFSP.