以下代码实现在Solidworks中构建草图,在草图中绘制了一个圆和一根样条曲线,并实现全约束。
import numpy as np
import win32com.client as win32
import pythoncom
def vtPoint(x, y, z):# 坐标点转化为浮点数return win32.VARIANT(pythoncom.VT_ARRAY | pythoncom.VT_R8, (x, y, z))def vtObj(obj):# 转化为对象数组return win32.VARIANT(pythoncom.VT_ARRAY | pythoncom.VT_DISPATCH, obj)def vtFloat(list):# 列表转化为浮点数return win32.VARIANT(pythoncom.VT_ARRAY | pythoncom.VT_R8, list)def vtInt(list):# 列表转化为整数return win32.VARIANT(pythoncom.VT_ARRAY | pythoncom.VT_I2, list)def vtVariant(list):# 列表转化为变体return win32.VARIANT(pythoncom.VT_ARRAY | pythoncom.VT_VARIANT, list)swApp = win32.Dispatch('sldworks.application')
swApp.Visible = True
Nothing = win32.VARIANT(pythoncom.VT_DISPATCH, None)
Part = swApp.NewDocument(r"C:\ProgramData\SolidWorks\SOLIDWORKS 2016\templates\gb_part.prtdot", 0, 0, 0)
# Part = swApp.ActiveDoc
boolstatus = Part.Extension.SelectByID2("前视基准面", "PLANE", 0, 0, 0, False, 0, Nothing, 0)
Part.SketchManager.InsertSketch(True)
Part.ClearSelection2(True)
Part.SketchManager.CreateCircle(0, 0, 0, 0.058688, 0.001201, 0)
Part.ClearSelection2(True)
Part.SketchManager.CreateCenterLine(-0.078937, 0, 0, 0.094381, 0, 0)
Part.ClearSelection2(True)
Part.SketchManager.CreateCenterLine(0, 0.085286, 0, 0, -0.084943, 0)
Part.ClearSelection2(True)
points=(-4.10648844451184E-02,4.19275921015285E-02,0,-2.47106162035851E-02,2.72846387247918E-02,0,1.23553081017925E-02,2.38526086965161E-02,0,0.03672272130255,2.52254207078264E-02,0,5.77327604333032E-02,1.05440071450045E-02,0)
pointsarray=vtFloat(points)
# pointsarray=win32.VARIANT(12,points)
Part.SketchManager.CreateSpline2(pointsarray,boolstatus)
Part.ClearSelection2(True)boolstatus = Part.Extension.SelectByID2("Point8", "SKETCHPOINT", -4.10648844451184E-02, 4.19275921015285E-02, 0, False, 0, Nothing, 0)
boolstatus = Part.Extension.SelectByID2("Line1", "SKETCHSEGMENT", -3.98739928799935E-02, 8.58007507068922E-04, 0, True, 0, Nothing, 0)
myDisplayDim = Part.AddDimension2(-0.110573811462473, 2.38526086965161E-02, 0)
Part.ClearSelection2(True)
boolstatus = Part.Extension.SelectByID2("Point10", "SKETCHPOINT", -2.47106162035851E-02, 2.72846387247918E-02, 0, False, 0, Nothing, 0)
boolstatus = Part.Extension.SelectByID2("Line1", "SKETCHSEGMENT", -3.98739928799935E-02, 8.58007507068922E-04, 0, True, 0, Nothing, 0)
myDisplayDim = Part.AddDimension2(-3.26667298206146E-02, 9.0948795749306E-03, 0)
Part.ClearSelection2(True)
boolstatus = Part.Extension.SelectByID2("Point11", "SKETCHPOINT", 1.23553081017925E-02, 2.38526086965161E-02, 0, False, 0, Nothing, 0)
boolstatus = Part.Extension.SelectByID2("Line1", "SKETCHSEGMENT", -3.98739928799935E-02, 8.58007507068922E-04, 0, True, 0, Nothing, 0)
myDisplayDim = Part.AddDimension2(1.67545025865555E-02, 7.72206756362032E-03, 0)
Part.ClearSelection2(True)
boolstatus = Part.Extension.SelectByID2("Point12", "SKETCHPOINT", 0.03672272130255, 2.52254207078264E-02, 0, False, 0, Nothing, 0)
boolstatus = Part.Extension.SelectByID2("Line1", "SKETCHSEGMENT", -3.98739928799935E-02, 8.58007507068922E-04, 0, True, 0, Nothing, 0)
myDisplayDim = Part.AddDimension2(3.32282467222789E-02, 0.011154097591896, 0)
Part.ClearSelection2(True)
boolstatus = Part.Extension.SelectByID2("Point9", "SKETCHPOINT", 5.77327604333032E-02, 1.05440071450045E-02, 0, False, 0, Nothing, 0)
boolstatus = Part.Extension.SelectByID2("Line1", "SKETCHSEGMENT", -3.98739928799935E-02, 8.58007507068922E-04, 0, True, 0, Nothing, 0)
myDisplayDim = Part.AddDimension2(8.81407271746901E-02, 0.013556518611689, 0)
Part.ClearSelection2(True)
boolstatus = Part.Extension.SelectByID2("Point9", "SKETCHPOINT", 5.77327604333032E-02, 1.05440071450045E-02, 0, False, 0, Nothing, 0)
boolstatus = Part.Extension.SelectByID2("Line2", "SKETCHSEGMENT", 2.80758450832141E-04, 0.047876818894446, 0, True, 0, Nothing, 0)
myDisplayDim = Part.AddDimension2(8.23062761266213E-02, 7.63626681291343E-02, 0)
Part.ClearSelection2(True)
boolstatus = Part.Extension.SelectByID2("Point12", "SKETCHPOINT", 0.03672272130255, 2.52254207078264E-02, 0, False, 0, Nothing, 0)
boolstatus = Part.Extension.SelectByID2("Line2", "SKETCHSEGMENT", 2.80758450832141E-04, 0.047876818894446, 0, True, 0, Nothing, 0)
myDisplayDim = Part.AddDimension2(8.02470581096559E-02, 5.95457209905834E-02, 0)
Part.ClearSelection2(True)
boolstatus = Part.Extension.SelectByID2("Point11", "SKETCHPOINT", 1.23553081017925E-02, 2.38526086965161E-02, 0, False, 0, Nothing, 0)
boolstatus = Part.Extension.SelectByID2("Line2", "SKETCHSEGMENT", 2.80758450832141E-04, 0.047876818894446, 0, True, 0, Nothing, 0)
myDisplayDim = Part.AddDimension2(2.56777806600723E-02, 4.47879918689979E-02, 0)
Part.ClearSelection2(True)
boolstatus = Part.Extension.SelectByID2("Point10", "SKETCHPOINT", -2.47106162035851E-02, 2.72846387247918E-02, 0, False, 0, Nothing, 0)
boolstatus = Part.Extension.SelectByID2("Line2", "SKETCHSEGMENT", 2.80758450832141E-04, 0.047876818894446, 0, True, 0, Nothing, 0)
myDisplayDim = Part.AddDimension2(-1.20745496509604E-02, 4.61608038803081E-02, 0)
Part.ClearSelection2(True)
boolstatus = Part.Extension.SelectByID2("Point8", "SKETCHPOINT", -4.10648844451184E-02, 4.19275921015285E-02, 0, False, 0, Nothing, 0)
boolstatus = Part.Extension.SelectByID2("Line2", "SKETCHSEGMENT", -3.98739928799935E-02, 8.58007507068922E-04, 0, True, 0, Nothing, 0)
myDisplayDim = Part.AddDimension2(-3.57555568460627E-02, 6.50369690358245E-02, 0)
Part.ClearSelection2(True)
boolstatus = Part.Extension.SelectByID2("Arc1", "SKETCHSEGMENT", -5.25725039846136E-02, -2.38526086965161E-02, 0, False, 0, Nothing, 0)
myDisplayDim = Part.AddDimension2(3.02638247345272E-03, -9.59252392903058E-02, 0)
Part.ClearSelection2(True)