vba 使用solidworks宏基于excel文件将零件插入装配体
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19645066/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Use solidworks macro to insert parts into assembly based on excel file
提问by cheapkid1
I am writing a macro that will eventually (hopefully!) read part numbers from an excel or text file, then search through the config's of my parts library and insert the corresponding components into an assembly, then make the corresponding config'n active.
我正在编写一个宏,最终(希望如此!)从 excel 或文本文件中读取零件编号,然后搜索零件库的配置并将相应的组件插入到组件中,然后激活相应的配置。
I have a problem when it comes to inserting the parts and or assemblies. I started off by recording a macro of inserting a part. Pretty simple. It uses the AddComponent command, which needs a filepath and x-y-z coordinates. This seemed to work ok but It kept glitching up. From what I've been able to figure out this command cannot insert a part or assembly UNLESS that part or assembly has already been used during the current session of solidworks.
我在插入零件和/或组件时遇到问题。我首先录制了插入零件的宏。很简单。它使用 AddComponent 命令,该命令需要文件路径和 xyz 坐标。这似乎工作正常,但它一直出现故障。据我所知,此命令无法插入零件或装配体,除非该零件或装配体已在当前 Solidworks 会话期间使用过。
Option Base 1
Dim swApp As Object
Dim Part As Object
Dim SelMgr As Object
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long
Dim Feature As Object
Dim filepath As String
Dim partnum(8) As String
Dim posx As Integer
Dim posy As Integer
Dim posz As Integer
Dim x As Integer
Sub main()
Set swApp = Application.SldWorks
Set Part = swApp.ActiveDoc
Set SelMgr = Part.SelectionManager
posx = 0
posy = 0
posz = 0
partnum(1) = "07010304"
partnum(2) = "07010318"
partnum(3) = "07010321"
partnum(4) = "07010331"
For x = 1 To 8
filepath = "C:\Documents and Settings\user\My Documents\Solid Works Testing\Parts\"+ partnum(x) + ".SLDPRT"
Part.AddComponent filepath, posx, posy, posz
filepath = "C:\Documents and Settings\user\My Documents\Solid Works Testing\Assemblies\" + partnum(x) + ".SLDASM"
Part.AddComponent filepath, posx, posy, posz
posx = posx + 1.5
Next
End Sub
This macro grabs three parts 07010304.SLDPRT, 07010318.SLDPRT, etc. an assembly (made up of those parts) and inserts them into the active assembly, spacing them out as it does so.
该宏抓取三个零件 07010304.SLDPRT、07010318.SLDPRT 等。一个装配(由这些零件组成)并将它们插入到活动装配中,同时将它们隔开。
- If I open an assembly and run the macro, nothing happens.
- If I open an assembly, insert the cube, delete it, then run the macro, it inserts the cube.
- If I open an assembly, insert all the parts, delete them, then run the macro, it inserts all the parts (but not the assembly made up of two of them).
- If I open an assembly, insert the cube-sphere assembly, delete it, and run the macro, it will insert the cube, the sphere, and the cube-sphere assembly, but not the cylinder.
- If the parts have been used in one assembly, and another is opened or made active, then the macro works fine in the new window.
- 如果我打开一个程序集并运行宏,则没有任何反应。
- 如果我打开一个程序集,插入多维数据集,删除它,然后运行宏,它会插入多维数据集。
- 如果我打开一个装配,插入所有零件,删除它们,然后运行宏,它会插入所有零件(但不是由两个零件组成的装配)。
- 如果我打开一个装配体,插入立方体球体装配体,删除它,然后运行宏,它将插入立方体、球体和立方体-球体装配体,而不是圆柱体。
- 如果零件已在一个装配中使用,而另一个已打开或激活,则宏在新窗口中正常工作。
So In conclusion, how do I fix this? Is there another command to insert parts that doesn't rely on the "loaded into SW memory" thing that seems to be happening? Or a command to do just that and load the parts into SW memory?
所以总而言之,我该如何解决这个问题?是否有另一个命令可以插入不依赖于似乎正在发生的“加载到软件内存中”的部分?或者只是执行此操作并将部件加载到软件内存中的命令?
回答by alphabit
First of all, you're missing the SolidWorks version you are using. So it is important that you know which API method variant you should use (i.e. in SolidWorks 2013 it would be AddComponent5 instead of AddComponent).
首先,您缺少正在使用的 SolidWorks 版本。因此,重要的是您知道应该使用哪种 API 方法变体(即在 SolidWorks 2013 中,它将是 AddComponent5 而不是 AddComponent)。
A part of this important detail, generally spoken, SolidWorks handles adding components to an assembly exactly as you have already noticed.
这个重要细节的一部分,一般来说,SolidWorks 完全按照您已经注意到的方式处理将零部件添加到装配体。
According to the SolidWorks API documentation, to add a component to an assembly, you first need to load that file into memory. A file is loaded into memory using the OpenDocmethod (please note that this depends on the version of SolidWorks you have, in 2013 you should use ISldWorks::OpenDoc6or ISldWorks::OpenDoc7).
根据 SolidWorks API 文档,要将零部件添加到装配体,您首先需要将该文件加载到内存中。使用OpenDoc方法将文件加载到内存中(请注意,这取决于您拥有的 SolidWorks 版本,2013 年您应该使用ISldWorks::OpenDoc6或ISldWorks::OpenDoc7)。
You should also take into consideration that calling ISldWorks::OpenDoc6doesn't activate & display the ModelDoc2 (document) if it is already open (in memory) in an assembly or drawing document.
您还应该考虑到调用ISldWorks::OpenDoc6不会激活和显示 ModelDoc2(文档),如果它已经在装配体或工程图文档中打开(在内存中)。
ISldWorks::OpenDoc6returns a reference to ModelDoc2 (IModelDoc2). Use the reference gained and pass it to ISldWorks::ActivateDoc2 or ISldWorks::IActivateDoc3 to activate and display.
ISldWorks::OpenDoc6返回对 ModelDoc2 (IModelDoc2) 的引用。使用得到的引用传递给ISldWorks::ActivateDoc2 或ISldWorks::IActivateDoc3 来激活和显示。
You should also take a look at the related OpenDoc events, like FileOpenNotify2, ActiveDocChangeNotify and ActiveModelDocChangeNotify to fine tune and control the loading and activation process of a document.
您还应该查看相关的 OpenDoc 事件,如 FileOpenNotify2、ActiveDocChangeNotify 和 ActiveModelDocChangeNotify,以微调和控制文档的加载和激活过程。
Here are some links to the related API documentation pages (API 2013):
以下是相关 API 文档页面 (API 2013) 的一些链接:
AddComponent5
添加组件5
OpenDoc6
OpenDoc6
OpenDoc7
OpenDoc7
AddComponent5 Example: Add component and mate
AddComponent5 示例:添加组件并配合
http://help.solidworks.com/2013/English/api/sldworksapi/add_component_and_mate_example_vb.htm
http://help.solidworks.com/2013/English/api/sldworksapi/add_component_and_mate_example_vb.htm