几何尺寸与公差论坛------致力于产品几何量公差标准GD&T (GDT:ASME)|New GPS(ISO)研究/CAD设计/CAM加工/CMM测量  


返回   几何尺寸与公差论坛------致力于产品几何量公差标准GD&T (GDT:ASME)|New GPS(ISO)研究/CAD设计/CAM加工/CMM测量 » 仿射空间:CAX软件开发(三)二次开发与程序设计 » CAD二次开发 » SolidWorks二次开发
用户名
密码
注册 帮助 会员 日历 银行 搜索 今日新帖 标记论坛为已读


回复
 
主题工具 搜索本主题 显示模式
旧 2009-04-12, 06:39 PM   #1
yang686526
高级会员
 
注册日期: 06-11
帖子: 14579
精华: 1
现金: 224494 标准币
资产: 234494 标准币
yang686526 向着好的方向发展
默认 【转帖】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.
yang686526离线中   回复时引用此帖
GDT自动化论坛(仅游客可见)
回复


主题工具 搜索本主题
搜索本主题:

高级搜索
显示模式

发帖规则
不可以发表新主题
不可以回复主题
不可以上传附件
不可以编辑您的帖子

vB 代码开启
[IMG]代码开启
HTML代码关闭

相似的主题
主题 主题发起者 论坛 回复 最后发表
How to connect to a running instance of SolidWorks in a Dispatch-based Executable huangyhg SolidWorks二次开发 0 2007-09-04 10:40 PM


所有的时间均为北京时间。 现在的时间是 01:11 PM.


于2004年创办,几何尺寸与公差论坛"致力于产品几何量公差标准GD&T | GPS研究/CAD设计/CAM加工/CMM测量"。免责声明:论坛严禁发布色情反动言论及有关违反国家法律法规内容!情节严重者提供其IP,并配合相关部门进行严厉查处,若內容有涉及侵权,请立即联系我们QQ:44671734。注:此论坛须管理员验证方可发帖。
沪ICP备06057009号-2
更多