0

我正在尝试使用 Python 3 在 SolidWorks 中自动创建一些零件(这里没有问题)并将这些零件添加到装配体中(这是我的问题)。

运行以下代码时没有任何错误,但部件未添加到程序集中。任何想法?

进口

import win32com.client
import pythoncom
from math import cos, sin, pi

创建零件

class PartModel():
    def __init__(self, cs=0., thk=0., c_opening=60., dj=0., name=""):

        self.sw = win32com.client.Dispatch('SldWorks.Application')
        self.sw.newpart

        self.model = self.sw.ActiveDoc
        self.modelExt = self.model.Extension
        self.selMgr = self.model.SelectionManager
        self.featureMgr = self.model.FeatureManager
        self.sketchMgr = self.model.SketchManager
        self.eqMgr = self.model.GetEquationMgr
        self.ARG_NULL = win32com.client.VARIANT(pythoncom.VT_DISPATCH, None)

        self.name = name
        self.dj = dj
        self.cs = cs
        self.thk = thk
        self.c_opening = c_opening

    def create_cring(self):
        """
        Create a simple c in SolidWorks
        """
        # make a new sketch on the Front Plane
        self.select_by_id2("Front Plane", "PLANE")
        self.insert_sketch()
        # sketch and dimension
        self.create_arc(X1=-cos(self.c_opening*pi/360)*(self.cs/2),
                        Y1=sin(self.c_opening*pi/360)*(self.cs/2),
                        X2=-cos(self.c_opening*pi/360)*(self.cs/2),
                        Y2=-sin(self.c_opening*pi/360)*(self.cs/2),
                        Direction=-1)
        self.create_arc(X1=-cos(self.c_opening*pi/360)*((self.cs-2*self.thk)/2),
                        Y1=sin(self.c_opening*pi/360)*((self.cs-2*self.thk)/2),
                        X2=-cos(self.c_opening*pi/360)*((self.cs-2*self.thk)/2),
                        Y2=-sin(self.c_opening*pi/360)*((self.cs-2*self.thk)/2),
                        Direction=-1)
        self.create_line(P1x=-cos(self.c_opening*pi/360)*(self.cs/2),
                         P1y=sin(self.c_opening*pi/360)*(self.cs/2),
                         P2x=-cos(self.c_opening*pi/360)*((self.cs-2*self.thk)/2),
                         P2y=sin(self.c_opening*pi/360)*((self.cs-2*self.thk)/2))
        self.create_line(P1x=-cos(self.c_opening*pi/360)*(self.cs/2),
                         P1y=-sin(self.c_opening*pi/360)*(self.cs/2),
                         P2x=-cos(self.c_opening*pi/360)*((self.cs-2*self.thk)/2),
                         P2y=-sin(self.c_opening*pi/360)*((self.cs-2*self.thk)/2))
        self.create_line(P1x=self.dj/2,
                         P1y=1,
                         P2x=self.dj/2,
                         P2y=-1,
                         cl=True)
        self.sketch_revolve()

        self.clear_selection()
        self.save_model(f_type="SLDPRT")

    def create_arc(self, XC=0., YC=0., Zc=0., X1=0., Y1=0., Z1=0., X2=0., Y2=0., Z2=0., Direction=-1):
        """
        :param XC: X coordinate of the circle center point in meters
        :param YC: Y coordinate of the circle center point in meters
        :param Zc: Z coordinate of the circle center point in meters
        :param X1: X coordinate of the start point of the arc in meters
        :param Y1: Y coordinate of the start point of the arc in meters
        :param Z1: Z coordinate of the start point of the arc in meters
        :param X2: X coordinate of the end point of the arc in meters
        :param Y2: Y coordinate of the end point of the arc in meters
        :param Z2: Z coordinate of the end point of the arc in meters
        :param Direction:
        +1 : Go from the start point to the end point in a counter-clockwise direction
        -1 : Go from the start point to the end point in a clockwise direction

        :return: Sketch arc
        """
        self.sketchMgr.CreateArc(XC, YC, Zc, X1, Y1, Z1, X2, Y2, Z2, Direction)

    def create_line(self, P1x=0., P1y=0., P1z=0., P2x=0., P2y=0., P2z=0., cl=False):
        """
        :param cl: Is the line a centerline ? Yes: True, No: False
        :param P1x: X value of the line start point
        :param P1y: Y value of the line start point
        :param P1z: Z value of the line start point
        :param P2x: X value of the line end point
        :param P2y: Y value of the line end point
        :param P2z: Z value of the line end point

        :return: Sketch line
        """
        if cl is True:
            self.sketchMgr.CreateCenterLine(P1x, P1y, P1z, P2x, P2y, P2z)
        if cl is False:
            self.sketchMgr.CreateLine(P1x, P1y, P1z, P2x, P2y, P2z)

    def insert_sketch(self):
        """
        insert sketch at plane
        """
        self.sketchMgr.InsertSketch(True)

    def select_by_id2(self, obj, obj_type, append=False):
        """
        Select different objects in order to preform operations on after
        Args:
            obj: Object name
            obj_type: Object type (ex. PLANE,SKETCH,SKECTCHSEGMENT)
            append: True to keep old selection, False to only keep newest
        """
        self.modelExt.SelectByID2(obj, obj_type, 0, 0, 0, append, 0, self.ARG_NULL, 0)

    def sketch_revolve(self):
        """
        Revolves a sketch
        """
        self.featureMgr.FeatureRevolve2(True, True, False, False, False, False, 0, 0, 2*pi, 0, False, False, 0.01, 0.01, 0, 0, 0, True, True, True)

    def clear_selection(self):
        """
        clears selected entities
        """
        self.model.ClearSelection2(True)

    def save_model(self, f_type="SLDPRT"):
        """
        Save CAD
        """
        self.model.SaveAs3(f"C:/temp/{self.name}.{f_type}", 0, 2)

创建装配体

class ASMModel():
    """
    Create the assembly in cad
    """

    def __init__(self, name="asm1"):
        self.sw_asm = win32com.client.Dispatch('SldWorks.Application')
        self.sw_asm.OpenDoc(f'C:/temp/Jacket.SLDPRT', 1)

        self.sw_asm = win32com.client.Dispatch('SldWorks.Application')
        self.sw_asm.newassembly
        self.model = self.sw_asm.ActiveDoc
        self.modelExt = self.model.Extension
        self.selMgr = self.model.SelectionManager
        self.featureMgr = self.model.FeatureManager
        self.sketchMgr = self.model.SketchManager
        self.eqMgr = self.model.GetEquationMgr
        self.ARG_NULL = win32com.client.VARIANT(pythoncom.VT_DISPATCH, None)

        self.name = name

    def create_asm(self):
        """
        Create a assembly in SolidWorks
        """

        self.clear_selection()
        self.save_model(f_type="SLDASM")

    def select_by_id2(self, obj, obj_type, append=False):
        """
        Select different objects in order to preform operations on after
        Args:
            obj: Object name
            obj_type: Object type (ex. PLANE,SKETCH,SKECTCHSEGMENT)
            append: True to keep old selection, False to only keep newest
        """
        self.modelExt.SelectByID2(obj, obj_type, 0, 0, 0, append, 0, self.ARG_NULL, 0)

    def clear_selection(self):
        """
        clears selected entities
        """
        self.model.ClearSelection2(True)

    def add_component(self):
        self.model.AddComponent4("C:/temp/Jacket.SLDPRT", "", 0., 0., 0.)

    def save_model(self, f_type="SLDASM"):
        """
        Save CAD
        """
        self.model.SaveAs3(f"C:/temp/{self.name}.{f_type}", 0, 2)

创建一个零件

jacket = PartModel(cs=4., thk=0.5, c_opening=60., dj=50., name="Jacket")
jacket.create_cring()

创建第二个

liner = PartModel(cs=3., thk=0.5, c_opening=60., dj=50., name="Lining")
liner.create_cring()

创建装配体

ass = ASMModel(name="asm1")
ass.create_asm()
ass.add_component()

谢谢 !

4

0 回答 0