python二次开发Solidworks:排雷以及如何排雷?

目录

1、重要文档

2、MSDN VARIANTS 

3、错误排除实例

3.1 第一个例子

3.2 第二个例子


1、重要文档

SolidWorks API 帮助文档:在 SolidWorks 的安装路径下可以找到本地文件,如 ...\Program Files\SolidWorks Corp\SolidWorks\api\apihelp.chm 。

swcommands.py 及 swconst.py 文件:由 makepy.py 程序生成的 swcommands.py 及 swconst.py 文件。在 ...\Lib\site-packages\win32com\client 文件夹内找到 makepy.py ,用 IDE 打开并运行,选择SOLIDWORKS 201x Type library和SOLIDWORKS 201x Constant type library的文件,然后重命名为swcommands.py 及 swconst.py。

2、MSDN VARIANTS 

typedef  enum tagVARENUM
 {
   VT_EMPTY = 0x0000,
   VT_NULL = 0x0001,
   VT_I2 = 0x0002,
   VT_I4 = 0x0003,
   VT_R4 = 0x0004,
   VT_R8 = 0x0005,
   VT_CY = 0x0006,
   VT_DATE = 0x0007,
   VT_BSTR = 0x0008,
   VT_DISPATCH = 0x0009,
   VT_ERROR = 0x000A,
   VT_BOOL = 0x000B,
   VT_VARIANT = 0x000C,
   VT_UNKNOWN = 0x000D,
   VT_DECIMAL = 0x000E,
   VT_I1 = 0x0010,
   VT_UI1 = 0x0011,
   VT_UI2 = 0x0012,
   VT_UI4 = 0x0013,
   VT_I8 = 0x0014,
   VT_UI8 = 0x0015,
   VT_INT = 0x0016,
   VT_UINT = 0x0017,
   VT_VOID = 0x0018,
   VT_HRESULT = 0x0019,
   VT_PTR = 0x001A,
   VT_SAFEARRAY = 0x001B,
   VT_CARRAY = 0x001C,
   VT_USERDEFINED = 0x001D,
   VT_LPSTR = 0x001E,
   VT_LPWSTR = 0x001F,
   VT_RECORD = 0x0024,
   VT_INT_PTR = 0x0025,
   VT_UINT_PTR = 0x0026,
   VT_ARRAY = 0x2000,
   VT_BYREF = 0x4000
 } VARENUM;

3、错误排除实例

3.1 第一个例子

出错代码:

modelExt.SelectByID2("mysketch", "SKETCH", 0, 0, 0, False, 0, None, 0)

错误类型:

Traceback (most recent call last): File "", line 1, in File ">", line 2, in SelectByID2 pywintypes.com_error: (-2147352571, 'Type mismatch.', None, 8)

类型不匹配中的最后一个数字。 行指示导致问题的参数。 在这种情况下,它是 None ,这是第八个参数。 API 手册页说它需要一个“指向标注的指针”。 我们只想传递 None,但 None 没有被转换为正确的 COM 对象,所以我们必须手动进行转换。

函数定义:

def SelectByID2(self, Name=defaultNamedNotOptArg, Type=defaultNamedNotOptArg, X=defaultNamedNotOptArg, Y=defaultNamedNotOptArg
			, Z=defaultNamedNotOptArg, Append=defaultNamedNotOptArg, Mark=defaultNamedNotOptArg, Callout=defaultNamedNotOptArg, SelectOption=defaultNamedNotOptArg):
		'Select a specified entity'
		return self._oleobj_.InvokeTypes(68, LCID, 1, (11, 0), ((8, 1), (8, 1), (5, 1), (5, 1), (5, 1), (11, 1), (3, 1), (9, 1), (3, 1)),Name
			, Type, X, Y, Z, Append
			, Mark, Callout, SelectOption)

元组列表( ((8,1), (8, 1), (5, 1), ...) 上面)包含有关每个参数的预期类型的信息。 第八个元组对应于有问题的第八个参数。 该元组是 (9, 1)。 现在在名为“VARIANT Type Constants”的 MSDN 网页中查找“9”(VT_DISPATCH = 0x0009,//十六进制的9),您会看到它与 VT_DISPATCH 匹配。 以下是如何手动生成正确对象的魔法:

arg1 = win32com.client.VARIANT(pythoncom.VT_DISPATCH, None)
modelExt.SelectByID2("Sketch1", "SKETCH", 0, 0, 0, False, 0, arg1, 0)

3.2 第二个例子

出错代码:

selMgr = model.SelectionManager
aSketch = selMgr.GetSelectedObject(1).GetSpecificFeature2
aSketch.GetReferenceEntity(0)

错误类型:

Traceback (most recent call last): File "", line 1, in File ">", line 2, in GetReferenceEntity pywintypes.com_error: (-2147352571, 'Type mismatch.', None, 1)

函数定义:

	def GetReferenceEntity(self, LEntityType=defaultNamedNotOptArg):
		'Get entity that this sketch is created on'
		return self._ApplyTypes_(52, 1, (9, 0), ((16387, 3),), u'GetReferenceEntity', None,LEntityType
			)

然后查看 VARIANT 网页,我们发现 16387 = pythoncom.VT_BYREF | pythoncom.VT_I4 所以 GetReferenceEntity 使用一个输出参数来返回实体类型。 我们可以构造一个类似于我们为 SelectByID2 所做的输出参数:

    arg1 = win32com.client.VARIANT(pythoncom.VT_BYREF | pythoncom.VT_I4, -1)
    refPlane = aSketch.GetReferenceEntity(arg1)

variant=win32com.client.VARIANT(16396, [1, 2, 3, 4, 5])

获取variant中的数据:variant.value

[1, 2, 3, 4, 5]

参考文献

1、Notion – The all-in-one workspace for your notes, tasks, wikis, and databases.

2、Library of macros and scripts to automate SOLIDWORKS

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是完整的 Python 代码。你可以将其复制到 Python 编辑器中,保存为 .py 文件,然后运行它来体验排雷游戏。 ```python import tkinter as tk import random # 创建窗口 root = tk.Tk() root.title("Minesweeper") root.geometry("500x500") # 创建标题 title_label = tk.Label(root, text="Minesweeper", font=("Arial", 24)) title_label.pack() # 创建棋盘 board = [] for i in range(10): row = [] for j in range(10): button = tk.Button(root, text="", width=3, height=2) button.grid(row=i+1, column=j+1) button.bind("<Button-1>", left_click) button.bind("<Button-3>", right_click) row.append(button) board.append(row) # 随机生成 25 个雷 mines = set() while len(mines) < 25: i = random.randint(0, 9) j = random.randint(0, 9) if (i, j) not in mines: mines.add((i, j)) # 左键点击事件 def left_click(event): button = event.widget i, j = get_index(button) if (i, j) in mines: button.config(text="X", bg="red") game_over() else: count = count_mines(i, j) button.config(text=count, bg="white") # 右键点击事件 def right_click(event): button = event.widget i, j = get_index(button) if button.cget("text") == "": button.config(text="*", fg="red") elif button.cget("text") == "*": button.config(text="?", fg="blue") else: button.config(text="", fg="black") # 计算周围的雷数 def count_mines(i, j): count = 0 for a, b in [(i-1, j-1), (i-1, j), (i-1, j+1), (i, j-1), (i, j+1), (i+1, j-1), (i+1, j), (i+1, j+1)]: if 0 <= a < 10 and 0 <= b < 10 and (a, b) in mines: count += 1 return count # 游戏结束 def game_over(): for i in range(10): for j in range(10): if (i, j) in mines: board[i][j].config(text="*", bg="white") else: count = count_mines(i, j) board[i][j].config(text=count, bg="white") # 获取按钮在二维数组中的索引 def get_index(button): for i in range(10): for j in range(10): if board[i][j] == button: return i, j # 启动游戏 root.mainloop() ``` 希望这个排雷游戏能够满足你的需求,有什么问题可以随时提出来。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值