Domanda

I'm trying to port over some code I wrote in VBA to control Solidworks in to Python. Specifically automating sketch edits. I am having problems using Solidworks SelectById2 in Python. In VBA the following code works fine:

Part.Extension.SelectByID2("Sketch1", "SKETCH", 0, 0, 0, False, 0, Nothing, 0)

The problem I am having is replacing VBA's "Nothing" value with some Python equivalent.

From the Solidworks API Docs, what SelectByID2 is looking for is:

SelectByID2(Name, Type, X, Y, Z, Append, Mark, Callout, SelectOption)

Where Callout is a pointer to the associated callout. I would prefer to not to create a pointer since it is not relevant to me and I have seen in VBA that it is not necessary.

I have tried using Python's None, pythoncom.Missing, pythoncom.Empty, "", " ", 0 ... all to no avail. All of these give me a TypeError.

Here is my relevant Python code:

import win32com.client
import pythoncom

pythoncom.CoInitializeEx(pythoncom.COINIT_APARTMENTTHREADED)

sldworks = win32com.client.gencache.EnsureModule('{83A33D31-27C5-11CE-BFD4-00400513BB57}', 0x0, 20, 0) # Solidworks OLE Automation 1.0 Type Library
swconst = win32com.client.gencache.EnsureModule('{4687F359-55D0-4CD3-B6CF-2EB42C11F989}', 0x0, 20, 0)  # Solidworks 2012 Constant Type Library

sw = sldworks.SldWorks()
sw.Visible = 1
model_path = "Y:\\Templates\\Solidworks\\test.SLDPRT"
doc, errors, warnings = sw.OpenDoc6(model_path, swconst.constants.swDocPART, swconst.constants.swOpenDocOptions_Silent, "", pythoncom.Missing, pythoncom.Missing)
sw.ActivateDoc2(model_path, False,pythoncom.Missing)
Part = sw.ActiveDoc  

try:
    Part.Extension.SelectByID2("Sketch1", "SKETCH", 0, 0, 0, False, 0, ffff, 0)
except Exception, value:        
    print "Exception occured, value = ", value 

Any suggestions on how to go about figuring this out?

È stato utile?

Soluzione 2

try changing the "ffff" to:

arg1 = win32com.client.VARIANT(pythoncom.VT_DISPATCH, None) 
to get:
doc.Extension.SelectByID2("Sketch1", "SKETCH", 0, 0, 0, False, 0, arg1, 0)

credit to: http://joshuaredstone.blogspot.com/2015/02/solidworks-macros-via-python.html

Altri suggerimenti

i know its not solving your problem, but maybe you can use a workaround with:

swModel.FirstFeature

then you ask for the name:

swFeat.Name = "Sketch1"

if its not the sketch you are looking for, head on for the next one:

swFeat = swFeat.GetNextFeature
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top