高级会员
注册日期: 06-11
帖子: 14579
精华: 1
现金: 224494 标准币
资产: 234494 标准币
|
【转帖】how to start a new instance of solidworks, hidden
how to start a new instance of solidworks, hidden
hi,
i've been trying (with no luck) to create a console application that will start up solidworks, open a drawing and save it as a pdf.
it appears to run just fine, however, that is not really the case.
my program does start up sldworks.exe but then it just hangs there, for about a minute, and then crashes with a nullreference exception.
this is the first time i try to develope for sw, so i have no experience with it.
this is the code that fails:
using solidworks.interop.sldworks;
using solidworks.interop.swcommands;
using solidworks.interop.swconst;
private bool dopdf(string inc)
{
bool success = false;
sldworks swapp = null;
console.writeline("before try");
try
{
int ierrors = 0;
int iwarnings = 0;
console.writeline("before new sldworks()");
swapp = new sldworks();
swapp.opendoc2(filename, 1, false, false, true, ref ierrors);
swapp.visible = true;
console.writeline("after opendoc");
}
catch (exception except)
{
throw except;
}
finally
{
swapp.exitapp();
swapp = null;
}
return success;
}
it never performs the console.writeline("after opendoc");.
am i missing something or doing something wrong here?
nicolai søndergaard
lm glasfiber a/s
answer i'm guessing try it will solidworks visible first to see if that solves the problem. also replacing the new sldworks() with:
swapp = system.activator.createinstance(system.type.gettypefromprogid("sldworks.application"));
thank you luke, that did the trick.
the documentation says (if i understand correctly) that you just need to do a sldworks swapp = new sldworks(); is that incorrect?
nicolai søndergaard
lm glasfiber a/s
its not incorrect but it isn't adviced it leaves the runtime to do all the work of converting and getting object types and the like from the "new ..." statement. the way i have showed you is the actual function that .net finally calls, which then calls the native cocreateinstance call which is the stopping point. the higher up the chain you go the more likelihood of problems.
is there a more direct way to close the application again then? i currently do a swapp.exitapp(); but quite often i see the sldworks.exe process still exist and have to be terminated manually.
is there a way to make sure it closes the program?
i have tried running it with swapp.visible = true, and when it reaches the exitapp, it appears to close down, but again, the process is still there.
nicolai søndergaard
lm glasfiber a/s
edited: 01/22/2009 at 08:54 am by nicolai søndergaard
i am hoping that luke or someone else has a better answer than my current solution.
dim mysolidworks() as process
mysolidworks = process.getprocessesbyname("sldworks")
if ubound(mysolidworks) > -1 then
bswxrunning = true
' connect to existing solidworks
else
' start new instance of solidworks
end if
' if solidworks is running already, do not close
if bswxrunning then
' make solidworks visible and give control to user
swapp.usercontrol = true
swapp.visible = true
else
' may have to kill solidworks
mysolidworks = process.getprocessesbyname("sldworks")
swapp.closealldocuments(true)
swapp.exitapp()
try
mysolidworks(0).kill()
catch ex as exception
end try
end if
wayne matus
texas engineering systems
the exitapp should always work if you have cleaned up your resources correctly. however, the only other way is to kill the process.
what you are doing is right but there is a better way to get a handle on the pid, i did it for another bit of software i developed last month i shall try and route it out when i get 5.
|