6.在文件MyPro.cpp的documentcallbacks部分修改菜单命令的执行函数;例如若建模等驱动程序为“D:\mypro\gs.exe”,则可将原来的菜单命令执行函数voidMenuItemCB(void) { return; } 修改为 voidMenuItemCB(void) { STARTUPINFOsi; PROCESSINFORMATIONpi; ZeroMemory(&si,sizeof(si)); si.cb=sizeof(si); ZeroMemory(&pi,sizeof(pi)); //Startthechildprocess. CreateProcess(“D:\mypro\gs.exe”, ””, //Commandline. NULL,//Processhandlenotinheritable. NULL,//Threadhandlenotinheritable. FALSE,//SethandleinheritancetoFALSE. 0,//Nocreationflags. NULL,//Useparent’senvironmentblock. NULL,//Useparent’sstartingdirectory. &si,//PointertoSTARTUPINFOstruc ture. &pi);//PointertoPROCESSINFORMA TIONstructure. return; }
7.编译工程并注册生成动态链接库[3]. 插件部分完成了图1中的a,b,c关系.尤其是函数CreateProcess的应用,实现了从菜单或工具条到建模驱动程序之间的连接,即图1中的关系c. 2.2.2 建模等驱动程序 下面以创建一个0.20m×0.10m×0.05m的长方体为例: 1.启动MicrosoftVisualBasic6.0,在下拉菜单“文件”项选择“新建工程”,选择“标准EXE”,“确定”; 2.在窗体Form1上添加2个CommandButton,分别命名为“创建长方体”和“退出”; 3.双击命令按钮“创建长方体”,在函数Com mand1Click()内添加代码如下: DimswAppAsObject DimPartAsObject DimboolstatusAsBoolean DimlongstatusAsLong DimAnnotationAsObject DimGtolAsObject DimDatumTagAsObject DimFeatureDataAsObject DimFeatureAsObject DimComponentAsObject DimchangAsLong,kuanAsLong,gaoAsLong‘定义长、宽、高 chang=0.20 ‘为长赋值0.20m kuan=0.10 ‘为宽赋值0.10m gao=0.05 ‘为高赋值0.05m SetswApp=CreateObject(“SldWorks.Applica tion”) swApp.ActiveDoc.ActiveView.FrameLeft=0 swApp.ActiveDoc.ActiveView.FrameTop=0 swApp.ActiveDoc.ActiveView.FrameState=1 swApp.ActiveDoc.ActiveView.FrameState=1 SetPart=swApp.NewDocument(“C:\ProgramFiles\SolidWorks\data\Templates\零件.prtdot”,0,0#,0#) SetPart=swApp.ActivateDoc(“Part2”) swApp.ActiveDoc.ActiveView.FrameState=1 Part.InsertSketch Part.SketchRectangle-(chang/2),(gao/2),0,(chang/2),(gao/2),0,1 Part.FeatureExtrusion1,0,0,0,0,kuan,0,0,0,0,0,0,0,0,0 Part.ShowNamedView2“ 等轴测”,7 Part.ViewZoomtofit2 Part.SaveAs2“D:\mypro\长方体.SLDPRT”,0,False,False Part.ClearSelection SetPart=Nothing swApp.CloseDoc“长方体” |