Hey everybody!
For starters, I want to say that I am rather new to the world of Autocad programming although I am experienced in .NET and a few other languages. Anyway, I need help with a specific scenario which I will describe followed by how I am trying to solve my issue. If there are easier ways, please let me know otherwise I just need a way to get my solution working.
To describe what I am trying to do, say I have an application which contains data on Autocad files. When the user alters data in my program, I want to open that dwg file and make changes to it based on what the user changed. If the user already has Autocad opened, I want to access it then search to see if they have this specific dwg opened and, if so, make the changes then and there. If they do not have the file opened, then a new instance of AutoCad needs to be created so I can then open the file, make my changes, then close it. So essentially I need to be able to accessa file and make the appropirate changes without the user having to do anything other than making a change in my application.
My solution was to create an Add-In for AutoCad which contains commands that allow me to make my changes. Currently, how it contains two commands with one being a command to collect data on what needs to be done and the other executes based on that data. This Add-In works perfectly when loading it manually through AutoCad and running the commands through the key in. Next, to get my application to be able to gain access to AutoCad I created a dll which gains access to AutoCad through the use of COM which then tells AutoCad to load my Add-In dll and then tells it to execute my commands. This works except here is where I run into my problem.
The dll used by my application which uses COM to access the AutoCad application uses <AutocCad Object>.ActiveDocument.SendCommand() to run my command to load my add in dll and then tells it to run the commands of that dll and passes the appropriate information. However, SendCommand is not a good way of going about this because it is unreliable. For starters, if the user is already doing something in AutoCad or has something typed into the command line when I attempt to tell it to load my Add-In then I get an exception. I've been trying to find a means to send the cancel command or even sending an ESC key press with things like SendMessage to the Hwnd but they don't work. I've been tryin to look everywhere online but can't seem to find a way to execute my commands with something other than SendCommand or even find a way to cancel out of what the user is doing through the COM interface.
So that is the issue I've been trying to solve for almost three days now (off and on). To give more information, I am working with VB.NET (solution must work from .NET and I also have a good understanding of C#) and am using the AutoCAD 2014 Type Library as my COM interface. I also have ObjectARX for .Net if that can help at all. I want my programs to work with at leastAutoCAD 2013 and higher (I do my initial testing with 2014 and once that is working I'll test other versions) and want this to be accomplished without having seperate dll's made to handle different versions. I am open to any solution so long as it falls within my requirements (my application talks to AutoCad through some means in .Net which makes the changes I require then saves the file).
Thanks!
EDIT:
I forgot to mention that I also have an alternative method where I use the AcCore Console to execute a script which runs my commands. However, that only works if the user does not have the dwg file opened. My final solution will probably end up being to check to see if an AutoCad instance is opened and if it has the dwg file I'm targetting opened then I have to use that instance of AutoCad to edit my file. Otherwise, I just use AcCore to edit the file from behind the scenes. Anyway, let us just ignore AcCore for the solution to my problem as it is isn't always applicable.
i had somkind of the same problem
first i tried to solve it with sendstringtoexecute but there u have the same problem that the command is async
at the end i found a solution for my problem
first put your command a commandflag.session
<CommandMethod("Command", Commandflags.Session)>
then make
Dim doc as Document = Application.DocumentMAnager.MdiActiveDocument
Using doc.LockDocument
'put here wathever u want acad to do
end using
doc=nothing
hope this works for u too
Josef