Pascal wrote on 3/5/2015 2:22 PM:
Hello,

I'm not an expert, I just want to experiment python with SolidWorks
I'm trying to use its DLL with some success, but unfortunately, with some errors :
File "<COMObject <unknown>>", line 2, in Get5
com_error: (-2147352571, 'Type mismatch', None, 5)

I'm not sure about error comment, it's a translation because my python is in french language.

Here is a simplified extract of code :

app = win32com.client.Dispatch('Sldworks.Application')
part = app.ActiveDoc

docext = part.Extension
custPropMgr = docext.CustomPropertyManager('ma_config')
pNames = tuple(custPropMgr.GetNames) # return array of strings

for item in pNames:
      print(item) # string is well printed
      var1=''
      var2=''
      var3=False
      custPropMgr.Get5(item, False, var1, var2, var3) <== error happens here

I tried several things but I didn't reach to pass correctly the parameters to the Get5 method.
I think the problem is with the ByRef parameters, because I already used many method with ByVal parameters and it works rather fine.

I tried : var1, var2, var3 = custPropMgr.Get5(item, False)  as suggested somewhere but it didn't works

this method is described as below in the solidworks API help:

Visual Basic (Declaration)  
Function Get5( _
   ByVal FieldName As System.String, _
   ByVal UseCached As System.Boolean, _
   ByRef ValOut As System.String, _
   ByRef ResolvedValOut As System.String, _
   ByRef WasResolved As System.Boolean _
) As System.Integer
Visual Basic (Usage)  
Dim instance As ICustomPropertyManager
Dim FieldName As System.String
Dim UseCached As System.Boolean
Dim ValOut As System.String
Dim ResolvedValOut As System.String
Dim WasResolved As System.Boolean
Dim value As System.Integer
 
value = instance.Get5(FieldName, UseCached, ValOut, ResolvedValOut, WasResolved)


I'm not much of an expert either, but I also use an API with many ByRef parameters in its method calls. The way I solved the problem was to use "early binding" of the COM objects.

To do that, you must first find the Type Library for your API. For SolidWorks, this may be "sldworks.tlb", although I can't really be sure. I see 20 "tlb" files in my
[SolidWorks_path]\SolidWorks folder, and I have no idea which one you might need, or even if you need all of them. I would start with "sldworks.tlb".

Next, from that type library, you create a Python file that you can import into your code. You do that with the "makepy" utility that comes with
the Python COM package.
1. Open a command window in the folder where you keep the Python code you're developing.
2. Enter the command
    [Python_path]\Lib\site-packages\win32com\client\makepy.py

      -v -o PySldworks.py [SolidWorks_path]\SolidWorks\sldworks.tlb
This will create a file called PySldworks.py (you can name it whatever you  want, of course). Now you can just add "Import PySldworks" to all the Import statements you already use.

Instead of
app = win32com.client.Dispatch('Sldworks.Application')

you have something like
app = PySldworks.SldWorks

Now those calls that return data using ByRef parameters should work with the Python-like form you tried because PySldworks.py has already defined it:
var1, var2, var3 = custPropMgr.Get5(item, False)

If that method has a return code, it will appear before the ByRef parameters:
rc, var1, var2, var3 = custPropMgr.Get5(item, False)

If that doesn't work, you can look in PySldworks.py to understand exactly what it thinks it should do with that method; it is, after all, just another Python file.

If there is another way to solve this problem, I don't know what it is. One of the real experts who monitor this list will have to help you with that.

- Greg Antal

------------------
Gregory W. Antal
Senior Technical Advisor
ATA Engineering, Inc.
13290 Evening Creek Drive South, Suite 250
San Diego, CA 92128
www.ata-e.com

greg.an...@ata-e.com
858-480-2072 (Phone)
858-792-8932 (Fax)
------------------
_______________________________________________
python-win32 mailing list
python-win32@python.org
https://mail.python.org/mailman/listinfo/python-win32

Reply via email to