|
樓主 |
發表於 2018/5/13 14:10:46
|
顯示全部樓層
原來Visual Studio也有這個問題啊,是我疏忽了,抱歉....
我將代碼貼在記事本附上
自動化生成零件.txt
(7.11 KB, 下載次數: 91)
自訂類別.txt
(4.14 KB, 下載次數: 75)
我把代碼貼在這
- Imports SolidWorks.Interop.sldworks
- Imports SolidWorks.Interop.swconst
- Public Class Form1
- '建立應用程式環境
- Dim SwApp As New SldWorks
- Dim Part As ModelDoc2
- Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
- '開啟應用程式 -> 建立零件
- SwApp.Visible = True
- Part = SwApp.INewPart
- '建立Part1
- CreatePart1()
- End Sub
- Sub CreatePart1()
- '建立零件文檔
- Dim Part As ModelDoc2 = SwApp.ActiveDoc
- Part = SwApp.INewPart
- '抑制尺寸設定對話框
- SwApp.SetUserPreferenceToggle(10, False)
- '填料1
- Part.Extension.SelectByID2("上基準面", "PLANE", 0, 0, 0, False, 0, Nothing, 0)
- Part.SketchManager.InsertSketch(True)
- Part.SketchManager.CreateCornerRectangle(-0.036, -0.0475, 0, 0.036, 0.0475, 0)
- Part.Extension.SelectByID2("Line2", "SKETCHSEGMENT", 0, 0, 0, False, 0, Nothing, 0)
- Part.SelectMidpoint()
- Part.Extension.SelectByID2("Point1@原點", "EXTSKETCHPOINT", 0, 0, 0, True, 0, Nothing, 0)
- Part.SketchAddConstraints("sgHORIZONTALPOINTS2D")
- Part.Extension.SelectByID2("Line1", "SKETCHSEGMENT", 0, 0, 0, False, 0, Nothing, 0)
- Part.SelectMidpoint()
- Part.Extension.SelectByID2("Point1@原點", "EXTSKETCHPOINT", 0, 0, 0, True, 0, Nothing, 0)
- Part.SketchAddConstraints("sgVERTICALPOINTS2D")
- Part.ClearSelection2(True)
- Part.Extension.SelectByID2("Line2", "SKETCHSEGMENT", 0, 0, 0, False, 0, Nothing, 0)
- Part.AddDimension2(-0.1, 0, 0)
- Part.ClearSelection2(True)
- Part.Extension.SelectByID2("Line1", "SKETCHSEGMENT", 0, 0, 0, False, 0, Nothing, 0)
- Part.AddDimension2(0, 0, 0.1)
- Part.SketchManager.InsertSketch(True)
- Part.Extension.SelectByID2("草圖1", "SKETCH", 0, 0, 0, False, 4, Nothing, 0)
- Part.FeatureManager.FeatureExtrusion2(True, False, False, 6, 0, 0.012, 0.01, False, False, False, False, 0, 0, False, False, False, False, True, True, True, 0, 0, False)
- Part.ViewZoomtofit2()
- '除料1
- Part.Extension.SelectByID2("", "FACE", 0.036, 0, 0, False, 0, Nothing, 0)
- Part.SketchManager.InsertSketch(True)
- Part.ClearSelection2(True)
- Part.SketchManager.CreatePolygon(-0.0295, 0.006551, 0, -0.022429, 0.006551, 0, 4, True)
- Part.ClearSelection2(True)
- Part.Extension.SelectByID2("Point5", "SKETCHPOINT", 0, 0, 0, False, 0, Nothing, 0)
- Part.Extension.SelectByID2("", "EDGE", 0.036, 0, 0.0475, True, 0, Nothing, 0)
- Part.AddDimension2(0.036, 0.02, 0.04)
- Part.ClearSelection2(True)
- Part.Extension.SelectByID2("Point5", "SKETCHPOINT", -0.0295, 0.006551, 0, False, 0, Nothing, 0)
- Part.Extension.SelectByID2("", "EDGE", 0.036, -0.006, 0, True, 0, Nothing, 0)
- Part.AddDimension2(0.036, 0, 0.06)
- Part.ClearSelection2(True)
- Part.Extension.SelectByID2("Arc1", "SKETCHSEGMENT", 0, 0, 0, False, 0, Nothing, 0)
- Part.AddDimension2(0.036, 0.015, 0.015)
- Part.ClearSelection2(True)
- Part.Extension.SelectByID2("Line3", "SKETCHSEGMENT", 0, 0, 0, False, 0, Nothing, 0)
- Part.Extension.SelectByID2("", "EDGE", 0.036, -0.006, 0, True, 0, Nothing, 0)
- Part.AddDimension2(0.036, -0.002, 0.035)
- Part.ClearSelection2(True)
- Part.SketchManager.InsertSketch(True)
- Part.Extension.SelectByID2("草圖2", "SKETCH", 0, 0, 0, False, 4, Nothing, 0)
- Part.FeatureManager.FeatureCut3(True, False, False, 1, 0, 0.01, 0.01, False, False, False, False, 0, 0, False, False, False, False, False, True, True, True, True, False, 0, 0, False)
- Part.ClearSelection2(True)
- Part.ViewZoomtofit2()
- '除料2
- Part.Extension.SelectByID2("", "FACE", 0, 0.006, 0.042, False, 0, Nothing, 0)
- Part.SketchManager.InsertSketch(True)
- Part.SketchManager.CreateCornerRectangle(-0.012, -0.0375, 0, 0.012, -0.0475, 0)
- Part.Extension.SelectByID2("Line3", "SKETCHSEGMENT", 0, 0, 0, False, 0, Nothing, 0)
- Part.SelectMidpoint()
- Part.Extension.SelectByID2("Point1@原點", "EXTSKETCHPOINT", 0, 0, 0, True, 0, Nothing, 0)
- Part.SketchAddConstraints("sgVERTICALPOINTS2D")
- Part.Extension.SelectByID2("Line4", "SKETCHSEGMENT", 0, 0, 0, False, 0, Nothing, 0)
- Part.AddDimension2(0.02, 0.006, 0.04)
- Part.ClearSelection2(True)
- Part.Extension.SelectByID2("Line1", "SKETCHSEGMENT", 0, 0, 0, False, 0, Nothing, 0)
- Part.AddDimension2(0, 0.006, 0.05)
- Part.ClearSelection2(True)
- Part.SketchManager.InsertSketch(True)
- Part.Extension.SelectByID2("草圖3", "SKETCH", 0, 0, 0, False, 4, Nothing, 0)
- Part.FeatureManager.FeatureCut3(True, False, False, 1, 0, 0.01, 0.01, False, False, False, False, 0, 0, False, False, False, False, False, True, True, True, True, False, 0, 0, False)
- Part.ClearSelection2(True)
- '沉頭孔1(嘗試改為定位銷孔)
- Dim Counterbores As New Hole(swWzdGeneralHoleTypes_e.swWzdCounterBore, swWzdHoleStandards_e.swStandardISO,
- swWzdHoleStandardFastenerTypes_e.swStandardISOSocketHeadCap, "M4",
- swEndConditions_e.swEndCondThroughAll, 0.0045, -1, -1, 0.008, 0.0045,
- -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, "", False, False, False, False, False, False)
- Part.Extension.SelectByID2("", "FACE", -0.018, 0.006, 0.0425, False, 0, Nothing, 0)
- Part.FeatureManager.HoleWizard5(Counterbores.GenericHoleType, Counterbores.StandardIndex, Counterbores.FastenerTypeIndex,
- Counterbores.SSize, Counterbores.EndType, Counterbores.Diameter, Counterbores.Depth, Counterbores.Length,
- Counterbores.Value1, Counterbores.Value2, Counterbores.Value3, Counterbores.Value4, Counterbores.Value5,
- Counterbores.Value6, Counterbores.Value7, Counterbores.Value8, Counterbores.Value9, Counterbores.Value10,
- Counterbores.Value11, Counterbores.Value12, Counterbores.ThreadClass, Counterbores.RevDir, Counterbores.FeatureScope,
- Counterbores.AutoSelect, Counterbores.AssemblyFeatureScope, Counterbores.AutoSelectComponents, Counterbores.PropagateFeatureToParts)
- Part.Extension.SelectByID2("草圖5", "SKETCH", 0, 0, 0, False, 0, Nothing, 0)
- Part.EditSketch()
- Part.SketchManager.CreatePoint(0.015, 0.0415, 0)
- Part.SketchManager.CreatePoint(-0.005, 0.0365, 0)
- Part.SketchManager.CreatePoint(-0.026, 0.0165, 0)
- Part.SketchManager.CreatePoint(0.026, 0.0165, 0)
- Part.SketchManager.CreatePoint(-0.031, 0.0015, 0)
- Part.SketchManager.CreatePoint(0.031, 0.0015, 0)
- Part.SketchManager.CreatePoint(-0.005, -0.0115, 0)
- Part.SketchManager.CreatePoint(0.015, -0.0165, 0)
- Part.SketchManager.CreatePoint(0.018, -0.0425, 0)
- Part.SketchManager.InsertSketch(True)
- '開啟尺寸設定對話框
- SwApp.SetUserPreferenceToggle(10, True)
- End Sub
複製代碼
為了方便在監看式中觀察鑽孔指令的參數,所以我把它包裝成自訂類別
- Public Class Hole
- Public Sub New(ByVal GenericHoleType As Integer, ByVal StandardIndex As Integer, ByVal FastenerTypeIndex As Integer, ByVal SSize As String,
- ByVal EndType As Integer, ByVal Diameter As Double, ByVal Depth As Double, ByVal Length As Double,
- ByVal Value1 As Double, ByVal Value2 As Double, ByVal Value3 As Double, ByVal Value4 As Double,
- ByVal Value5 As Double, ByVal Value6 As Double, ByVal Value7 As Double, ByVal Value8 As Double,
- ByVal Value9 As Double, ByVal Value10 As Double, ByVal Value11 As Double, ByVal Value12 As Double,
- ByVal ThreadClass As String, ByVal RevDir As Boolean, ByVal FeatureScope As Boolean, ByVal AutoSelect As Boolean,
- ByVal AssemblyFeatureScope As Boolean, ByVal AutoSelectComponents As Boolean, ByVal PropagateFeatureToParts As Boolean)
- Me.GenericHoleType = GenericHoleType
- Me.StandardIndex = StandardIndex
- Me.FastenerTypeIndex = FastenerTypeIndex
- Me.SSize = SSize
- Me.EndType = EndType
- Me.Diameter = Diameter
- Me.Depth = Depth
- Me.Length = Length
- Me.Value1 = Value1
- Me.Value2 = Value2
- Me.Value3 = Value3
- Me.Value4 = Value4
- Me.Value5 = Value5
- Me.Value6 = Value6
- Me.Value7 = Value7
- Me.Value8 = Value8
- Me.Value9 = Value9
- Me.Value10 = Value10
- Me.Value11 = Value11
- Me.Value12 = Value12
- Me.ThreadClass = ThreadClass
- Me.RevDir = RevDir
- Me.FeatureScope = FeatureScope
- Me.AutoSelect = AutoSelect
- Me.AssemblyFeatureScope = AssemblyFeatureScope
- Me.AutoSelectComponents = AutoSelectComponents
- Me.PropagateFeatureToParts = PropagateFeatureToParts
- End Sub
- Public GenericHoleType As Integer
- '柱孔 swWzdGeneralHoleTypes_e.swWzdCounterBore
- '錐孔 swWzdGeneralHoleTypes_e.swWzdCounterSink
- '鑽孔 swWzdGeneralHoleTypes_e.swWzdHole
- '直螺絲孔 swWzdGeneralHoleTypes_e.swWzdTap
- '斜螺絲孔 swWzdGeneralHoleTypes_e.swWzdPipeTap
- '舊制孔 swWzdGeneralHoleTypes_e.swWzdLegacy
- '柱孔狹槽 swWzdGeneralHoleTypes_e.swWzdCounterBoreSlot
- '錐孔狹槽 swWzdGeneralHoleTypes_e.swWzdCounterSinkSlot
- '狹槽 swWzdGeneralHoleTypes_e.swWzdHoleSlot
- Public StandardIndex As Integer
- 'ISO(公制) swWzdHoleStandards_e.swStandardISO
- Public FastenerTypeIndex As Integer
- '公制沉頭孔 swWzdHoleStandardFastenerTypes_e.swStandardISOSocketHeadCap
- '公制定位孔 swWzdHoleStandardFastenerTypes_e.swStandardISODowelHole
- Public SSize As String
- '特徵描述(特徵樹提示文字)
- Public EndType As Integer
- '特徵終止型態
- 'swEndConditions_e.swEndCondBlind 指定深度
- 'swEndConditions_e.swEndCondThroughAll 完全貫穿
- 'swEndConditions_e.swEndCondThroughNext 成形至下一面
- 'swEndConditions_e.swEndCondUpToVertex 成形至一頂點
- 'swEndConditions_e.swEndCondUpToSurface 成形至某一面
- 'swEndConditions_e.swEndCondOffsetFromSurface 至某面平移處
- 'swEndConditions_e.swEndCondMidPlane 兩側對稱
- 'swEndConditions_e.swEndCondUpToBody 成形至本體
- 'swEndConditions_e.swEndCondThroughAllBoth 完全貫穿-兩者
- Public Diameter As Double
- '鑽孔直徑
- Public Depth As Double
- '鑽孔深度
- Public Length As Double
- '狹槽長度(只對於狹槽型態的孔有效)
- Public Value1 As Double
- Public Value2 As Double
- Public Value3 As Double
- Public Value4 As Double
- Public Value5 As Double
- Public Value6 As Double
- Public Value7 As Double
- Public Value8 As Double
- Public Value9 As Double
- Public Value10 As Double
- Public Value11 As Double
- Public Value12 As Double
- '依鑽孔類型有不同的定義,忽略該數值請輸入-1
- Public ThreadClass As String
- Public RevDir As Boolean
- Public FeatureScope As Boolean
- Public AutoSelect As Boolean
- Public AssemblyFeatureScope As Boolean
- Public AutoSelectComponents As Boolean
- Public PropagateFeatureToParts As Boolean
- End Class
複製代碼
謝謝你提供的巨集,晚點我來試試看
你的巨集錄完之後可以執行鑽孔嗎,不知道為啥我的不行...
|
|