首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >我可以在Visual Studio2017中使用IronPython并创建Ironpython WPF应用程序来将自定义属性变量传递给Solidworks吗?

我可以在Visual Studio2017中使用IronPython并创建Ironpython WPF应用程序来将自定义属性变量传递给Solidworks吗?
EN

Stack Overflow用户
提问于 2019-03-26 01:33:32
回答 1查看 470关注 0票数 1

我正在使用IronPython 2.7.9在Visual Studio2017上创建自己的WPF应用程序。我想连接到激活的Solidworks应用程序,并将自定义属性值作为字符串传递到激活的零件或组件。

WPF应用程序将检查哪个文件是打开的。在此之后,它将更新为已作为自定义属性写入应用程序的应用程序值。通过修改这些值并保存,我会将它们写入Solidworks零件或组件。

我的第一步是创建到正在运行的Solidworks的连接,获取活动文档文件名并将其显示在我的应用程序的textbox上。

我接触到的第一个问题是,没有关于连接到IronPython上的Solidworks应用程序的正确方法的信息。实际上,IronPython不支持Solidworks API中引用的早期绑定。Solidworks自带了自己的API DLL文件。

我已经使用了Visual Studio 2017 Ironpython WPF应用程序项目。我已经在解决方案资源管理器中添加了\SOLIDWORKS\api\redist的搜索路径。在此之后,我已经启动了代码:

代码语言:javascript
运行
复制
python

import clr

clr.AddReference("SldWorks.Interop.sldworks")

import SldWorks.Interop.sldworks as SldWorks

swApp = SldWorks.SldWorks   # Get from here the active document

swModel = SldWorks.ModelDoc2   # Get string through GetTitle() from here

print(swModel.GetTitle(swApp.ActiveDoc))

我期望从活动的Solidworks会话中获得活动的文档标题。然后把这个打印出来。

当通过定义了sys.path.append的IronPython 2.7交互窗口运行时,最后一行给出了TypeError: expected IModelDoc2, got getset_descriptor

更新:到目前为止,我已经用自己的方式编写了这类代码。创建继承ModelDoc2类属性的类:

代码语言:javascript
运行
复制
import clr
import sys
import System

sys.path.append(r"C:\Program Files\SOLIDWORKS Corp\SOLIDWORKS\api\redist")
clr.AddReference("SolidWorks.Interop.sldworks")
import SolidWorks.Interop.sldworks as SldWorks


class ModelDoc(SldWorks.ModelDoc2):
   def getActiveDocumentTitle(self):
      self.str = SldWorks.ModelDoc2.GetTitle(SldWorks.IModelDoc2)
      return self.str

 swApp = ModelDoc()
 print(swApp.getActiveDocumentTitle())

问题还是一样的。我得到了

代码语言:javascript
运行
复制
Traceback (most recent call last):
    File "<string>", line 1, in <module>
    File "<string>", line 3, in getActiveDocumentTitle
 TypeError: expected IModelDoc2, got type

SOLIDWORKS是一个基于COM的API,它使用:

代码语言:javascript
运行
复制
Interfaces

Interface inheritance

Factory methods to return interfaces on existing and new objects

Casting between interfaces through:
    QueryInterface (C++), which returns a pointer to a specified interface on an object to which a client currently holds an interface pointer.
    direct assignment (VB/VB.NET).
    the is/as reserved words (C#).
EN

回答 1

Stack Overflow用户

发布于 2019-03-26 20:53:41

我认为应该是这样的:

代码语言:javascript
运行
复制
import clr
import sys
import System

sys.path.append(r"C:\Program Files\SOLIDWORKS Corp\SOLIDWORKS\api\redist")
clr.AddReference("SolidWorks.Interop.sldworks")
import SolidWorks.Interop.sldworks as SldWorks

swApp = System.Runtime.InteropServices.Marshal.GetActiveObject("SldWorks.Application")
swModel = swApp.ActiveDoc
print(swModel.GetTitle())

下面是在C#上类似的工作代码

代码语言:javascript
运行
复制
    SldWorks swApp;
    swApp = (SldWorks)System.Runtime.InteropServices.Marshal.GetActiveObject("SldWorks.Application");
    //swApp = (SldWorks)Activator.CreateInstance(System.Type.GetTypeFromProgID("SldWorks.Application"));
    ModelDoc2 doc = swApp.ActiveDoc;
    var str = doc.GetTitle();
    Console.WriteLine(str);

也请看看这篇文章,里面有关于从独立应用程序访问SolidWorks的有用信息:https://forum.solidworks.com/thread/215594

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55343521

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档