使用ISldWorks
open SolidWorks.Interop.sldworks
let cmdConnect (swApp: ISldWorks) =
swApp.SendMsgToUser $"RevisionNumber:{swApp.RevisionNumber}"
swApp.DisplayStatusBar true
swApp.SendMsgToUser $"DisplayStatusBar Ok!"
let lang = swApp.GetCurrentLanguage()
swApp.SendMsgToUser $"SOLIDWORKS is currently using the {lang} language."
新建零件:
let cmdNewModel_Part (swApp: ISldWorks) =
let swModel =
swApp.NewDocument(Path.Combine(TEMPLATEDIR, "gb_part.prtdot"), 0, 0.0, 0.0)
|> unbox<IModelDoc2>
swModel.InsertFamilyTableNew()
swApp.SendMsgToUser "InsertFamilyTableNew"
let text = "Sample Note"
let swNote =
swModel.InsertNote(text)
|> unbox<INote>
let swAnnotation =
swNote.GetAnnotation()
|> unbox<IAnnotation>
swAnnotation.SetPosition(0.01, 0.01, 0.0)
|> ignore
swApp.SendMsgToUser "Note"
新建装配体
let cmdNewModel_ASM (swApp: ISldWorks) =
let swModel =
swApp.NewDocument(Path.Combine(TEMPLATEDIR, "gb_assembly.asmdot"), 0, 0.0, 0.0)
|> unbox<IModelDoc2>
swModel.InsertFamilyTableNew()
swApp.SendMsgToUser "InsertFamilyTableNew"
let text = "Sample Note"
let swNote =
swModel.InsertNote(text)
|> unbox<INote>
let swAnnotation =
swNote.GetAnnotation()
|> unbox<IAnnotation>
swAnnotation.SetPosition(0.01, 0.01, 0.0)
|> ignore
swApp.SendMsgToUser text
新建工程图:
let cmdNewModel_DRW (swApp: ISldWorks) =
let swModel =
swApp.NewDocument(Path.Combine(TEMPLATEDIR, "gb_a1.drwdot"), 0, 0.0, 0.0)
|> unbox<IModelDoc2>
let drawname = swModel.GetTitle()
let partpath = @"C:\Users\cuisl\Documents\cstick.SLDPRT"
let _ =
let mutable errors = 0
let mutable warnings = 0
swApp.OpenDoc6(partpath, 1, 0, "", &errors, &warnings)
let swDrw =
let mutable errors = 0
swApp.ActivateDoc3(drawname, false,int swRebuildOnActivation_e.swRebuildActiveDoc, &errors)
|> unbox<IDrawingDoc>
swDrw.EditSheet()
let preparedrawingView() =
swDrw.CreateDrawViewFromModelView3(partpath,"*Isometric", 0.1, 0.1, 0.0)
let swView = preparedrawingView()
swView.FocusLocked <- true
swModel.Extension.SelectByID2(swView.GetName2(), "DRAWINGVIEW", 0, 0, 0, false, 0, null, int swSelectOption_e.swSelectOptionDefault)
|> ignore
swModel.InsertFamilyTableNew()
swApp.SendMsgToUser "InsertFamilyTableNew"
let text = "Sample Note"
let swNote =
swModel.InsertNote(text)
|> unbox<INote>
let swAnnotation =
swNote.GetAnnotation()
|> unbox<IAnnotation>
swAnnotation.SetPosition(0.01, 0.01, 0.0)
|> ignore
swApp.SendMsgToUser text
步骤 23
let cmdPart_Click (swApp: ISldWorks) =
let swModel =
swApp.NewDocument(Path.Combine(TEMPLATEDIR, "gb_part.prtdot"), 0, 0.0, 0.0)
|> unbox<IModelDoc2>
let swPart = swModel :?> IPartDoc
swModel.SketchManager.InsertSketch true
let _ = swModel.SketchManager.CreateCornerRectangle(0, 0, 0, 0.1, 0.1, 0)
let _ = swModel.FeatureManager.FeatureExtrusion2( true, false,
false, 0, 0, 0.1, 0.01, false, false, false, false,
0.01745329251994, 0.01745329251994, false, false,
false, false, true, true, true, 0, 0, false)
swPart.EditRollback()
步骤 24
let cmdAssy_Click (swApp: ISldWorks) =
let mutable fileerror = 0
let mutable filewarning = 0
let _ =
swApp.OpenDoc6(partpath, int swDocumentTypes_e.swDocPART, int swOpenDocOptions_e.swOpenDocOptions_Silent, "", &fileerror, &filewarning)
let swModel=
swApp.NewDocument(Path.Combine(TEMPLATEDIR, "gb_assembly.asmdot"), 0, 0.0, 0.0)
|> unbox<IModelDoc2>
let swAssy = swModel :?> IAssemblyDoc
let _ =
swAssy.AddComponent5(partpath,int swAddComponentConfigOptions_e.swAddComponentConfigOptions_CurrentSelectedConfig, "", false, "", 0.0, 0.0, 0.0)
()
步骤 25
let cmdDraw_Click (swApp: ISldWorks) =
let swDrw =
swApp.NewDocument(Path.Combine(TEMPLATEDIR, "gb_a1.drwdot"), 0, 0.0, 0.0)
|> unbox<IDrawingDoc>
let msg =
if swDrw.GetEditSheet() then
"sheet"
else "template"
swApp.SendMsgToUser msg
//swDrw.EditTemplate()
//swDrw.EditSheet()
()
2.4 实例学习,连接到现有文件
training2_frmExistingDocs
步骤 29
let cmdConnect_Click (swApp: ISldWorks) =
//If chkOpen.Value = True Then
let mutable fileerror = 0
let mutable filewarning = 0
let _ =
swApp.OpenDoc6(partpath, int swDocumentTypes_e.swDocPART, int swOpenDocOptions_e.swOpenDocOptions_Silent, "", &fileerror, &filewarning)
//If chkLoad.Value = True Then
let mutable errors = 0
let ImportedModelDoc =
swApp.LoadFile4(Path.Combine(TRAININGDIR, "cstick.igs"), "", null, &errors)
//If chkNewWindow.Value = True Then
swApp.CreateNewWindow()
swApp.ArrangeWindows 1
步骤 33
let cmdNewModel_Click(swApp: ISldWorks) =
let swModel = swApp.ActiveDoc |> unbox<IModelDoc2>
let toolbars =
[
swToolbar_e.swFeatureToolbar
swToolbar_e.swMacroToolbar
swToolbar_e.swMainToolbar
swToolbar_e.swSketchToolbar
swToolbar_e.swSketchToolsToolbar
swToolbar_e.swStandardToolbar
swToolbar_e.swStandardViewsToolbar
swToolbar_e.swViewToolbar
]
toolbars
|> Seq.iter(fun tb ->
swModel.SetToolbarVisibility(int tb,false)
)
let custPropMan = swModel.Extension.CustomPropertyManager("")
let _ = custPropMan.Add3("MyInfo", int swCustomInfoType_e.swCustomInfoText, "MyData", int swCustomPropertyAddOption_e.swCustomPropertyDeleteAndAdd)
()
步骤 36
//注意:源零件不能只有焊件
let cmdPart_Click(swApp: ISldWorks) =
let swModel = swApp.ActiveDoc |> unbox<IModelDoc2>
let swPart = swModel :?> IPartDoc
//执行之前保存原文件名
let partname = swModel.GetTitle()
let _ = swModel.Extension.SelectByID2("Top", "PLANE", 0, 0, 0, false, 0, null, 0)
let mirFeat,mirModel =
swPart.MirrorPart2(false,int swMirrorPartOptions_e.swMirrorPartOptions_ImportSolids)
mirModel.ShowNamedView2("*Isometric", 7)
mirModel.ViewZoomtofit2()
swApp.ArrangeWindows 1
// 重新选中源文件
let swModel =
let mutable errors = 0
swApp.ActivateDoc3(partname, false,int swRebuildOnActivation_e.swRebuildActiveDoc, &errors)
|> unbox<ModelDoc2>
let _ = swModel.DeSelectByID("Top", "PLANE", 0.0, 0.0, 0.0)
()
整数与布尔型的转化:
System.Convert.ToBoolean(1);;
val it: bool = true
步骤 39
let cmdAssy_Click(swApp: ISldWorks) =
let swModel = swApp.ActiveDoc |> unbox<ModelDoc2>
let swAssy = swModel :?> AssemblyDoc
let assyname = Path.GetFileNameWithoutExtension(swModel.GetTitle())
// EditPart2 sheetmetalsample-1
let boolstatus =
swModel.Extension.SelectByID2(
$"sheetmetalsample-1@{assyname}", "COMPONENT", 0, 0, 0,
false, 0, null, 0)
let mutable info = 0
let _ = swAssy.EditPart2(true, false, &info)
// InsertCavity4 plug-1
swModel.ClearSelection2 true
let boolstatus =
swModel.Extension.SelectByID2(
$"plug-1@{assyname}", "COMPONENT", 0, 0, 0,
true, 0, null, 0)
swAssy.InsertCavity4(10, 10, 10, true, 1, -1)
// EditSuppress2 plug-1
swAssy.EditAssembly()
let _ =
swModel.Extension.SelectByID2(
"plug-1@sheetmetalsample", "COMPONENT", 0, 0, 0,
true, 0, null, 0)
swModel.EditSuppress2()
|> ignore
步骤 42
open System.Drawing
let cmdDraw_Click (swApp: ISldWorks) =
let swModel = swApp.ActiveDoc |> unbox<ModelDoc2>
let swDraw = swModel :?> DrawingDoc
swModel.ClearSelection2 true
let retval =
swDraw.CreateLayer2("MyRedLayer", "Red", Color.FromArgb(255, 0, 0).ToArgb(),
int swLineStyles_e.swLineSTITCH, int swLineWeights_e .swLW_THICK, true, true)
()