-
Re: macro to export center of mass/gravity of parts from an assembly to excel
Deepak Gupta Nov 30, 2012 12:41 AM (in response to Mark Thompson)Mark did you looked at this macro by Tobin: https://forum.solidworks.com/message/215433#215433
-
Re: macro to export center of mass/gravity of parts from an assembly to excel
Mark Thompson Dec 3, 2012 7:13 AM (in response to Deepak Gupta)I've had a look at the macro and it seems very promising, however, since I don’t know how to address the first action “1) Reference to Microsoft Excel (version) Object Library”, I get the following error:
"Run-Time error '91'
Object variable or With block variable not set"
I honestly don't know whether this is because I'm running a more recent version of solidworks (2012) or because I have no idea how to set up the macro...
Also it appears this marco only references the CG of the entire assembly rather than each individual part relative to a common reference point (i.e. origin). Is there any way to expand this to the latter and get a exel file listing each individual part relative to the origin?
-
Re: macro to export center of mass/gravity of parts from an assembly to excel
Deepak Gupta Dec 3, 2012 7:18 AM (in response to Mark Thompson)Mark Thompson wrote:
I've had a look at the macro and it seems very promising, however, since I don’t know how to address the first action “1) Reference to Microsoft Excel (version) Object Library”, I get the following error:
"Run-Time error '91'
Object variable or With block variable not set"
For this issue, you need to fix missing libraries in the macro. Check this post on how to do that: https://forum.solidworks.com/message/328597#328597
Also it appears this marco only references the CG of the entire assembly rather than each individual part relative to a common reference point (i.e. origin). Is there any way to expand this to the latter and get a exel file listing each individual part relative to the origin?
I'm sure yes, this can be done. The macro will have to edited to get this done.
-
Re: macro to export center of mass/gravity of parts from an assembly to excel
Mark Thompson Dec 3, 2012 9:31 AM (in response to Deepak Gupta)Apologies, I should clarify, I've already tried edit macro>tools>references>Microsoft Excel 14.0 Object Library, both checked and uncheck, it still returns the same error. I've also checked the three references you pointed out in your post, however, Missing:Sldworks 2006 type library isn't present in the list. Not surprising since I run 2012 but there doesn't appear to be an equivalent.
As for editing the macro, this is unfortunately beyond me, is there a forum resource I can contact for help with this?
Thank you in advance.
Mark,
-
Re: macro to export center of mass/gravity of parts from an assembly to excel
Deepak Gupta Dec 3, 2012 10:51 AM (in response to Mark Thompson)Please make sure you've only selected libraries as shown in the attached picture selected. Uncheck any missing libraries (in case you see it in list). And Microsoft Excel library 14.0 is for MS office 2010. In case you've 2007, it will be 12.0.
After you do this, follow the instructions in the COG.text file (you'll find that in the extracted files).
And regarding modification in the macro, I'm sure someone will jump in to do. I'll also give it a try.
-
COG Libraries.PNG 27.4 KB
-
Re: macro to export center of mass/gravity of parts from an assembly to excel
Mark Thompson Dec 3, 2012 1:13 PM (in response to Deepak Gupta)Evening Deepak,
Again it appears I've already done this, please see attached:
Am I missing something? Do I need to create an excel file, name it, save it to a specific file location and reference it in the macro?
Thanks,
Mark.
-
-
Re: macro to export center of mass/gravity of parts from an assembly to excel
Deepak Gupta Dec 4, 2012 1:19 AM (in response to Mark Thompson)Found the issue. It is not reference issue but macro module itself. Main module needs to run first. Set a macro button and set that to main module and try again. I've attached a video on how to do it.
-
Running COG macro.mp4 648.6 KB
-
Re: macro to export center of mass/gravity of parts from an assembly to excel
Mark Thompson Dec 5, 2012 11:15 AM (in response to Deepak Gupta)Fantastic, that got it working!
This is great but I'm going to have a play and see if I can modify it further to output the part name, mass and x,y,z, location (relative to assembly origin) of each part in an assembly to an exel file. Fingers crossed! (any help/input would be very appreciated...)
Thank you in advance.
Mark.
-
Re: macro to export center of mass/gravity of parts from an assembly to excel
Deepak Gupta Dec 6, 2012 5:17 AM (in response to Mark Thompson)I've been able to export the file name, mass and COG to the excel file but still need to figure out as how to use the assembly origin/coordinate system
-
Re: macro to export center of mass/gravity of parts from an assembly to excel
Mark Thompson Dec 7, 2012 7:25 AM (in response to Deepak Gupta)SO CLOSE! May I see your code so far? Mine is rather buggy...
-
Re: macro to export center of mass/gravity of parts from an assembly to excel
Deepak Gupta Dec 7, 2012 8:24 AM (in response to Mark Thompson)It is bit messed up, let me clean it a bit and then I would post them here.
-
Re: macro to export center of mass/gravity of parts from an assembly to excel
Mark Thompson Dec 10, 2012 10:59 AM (in response to Deepak Gupta)Fantastic! Thank you.
-
Re: macro to export center of mass/gravity of parts from an assembly to excel
Deepak Gupta Dec 10, 2012 12:12 PM (in response to Mark Thompson)Sorry for the delay, here you go.
You might have to set the libraries in the macro.
-
Re: macro to export center of mass/gravity of parts from an assembly to excel
Mark Thompson Dec 13, 2012 9:32 AM (in response to Deepak Gupta)Afternoon Deepak,
Thats fantastic, thank you so much for your help! However, I do have one small issue when I try to run it... I follow the same instructions as before, that is, checking the reference library and running the main module first but when I do I get the following error:
It does open up an excel table and populate it with the correct column headings but obviously it can't get further to populate it with parametric data:
Although I'm not sure what it does, I can't seem to find and select one of the reference libraries, specifically 'Microsoft Forms 2.0 Object Library', please see attached image:
Any thoughts?
-
Re: macro to export center of mass/gravity of parts from an assembly to excel
Deepak Gupta Dec 13, 2012 10:38 AM (in response to Mark Thompson)Edit the macro and remove these lines
Dim swCoordSys As SldWorks.Feature
Dim csystransform As SldWorks.MathTransform
Set swMathUtils = swApp.GetMathUtility
Set swXForm = swComp.Transform2
Set swMathPt = swMathUtils.CreatePoint(nPt)
Set swMathPt = swMathPt.MultiplyTransform(swXForm)
SetCoordinates = swMathPt.ArrayData()
Set csystransform = SetCoordinates
swMassProps.SetCoordinateSystem (csystransform)
I was trying these lines to give back some data for setting up assembly origin/coordinates.
And references you've selected should be good to use the macro.
-
Re: macro to export center of mass/gravity of parts from an assembly to excel
Mark Thompson Dec 14, 2012 6:02 AM (in response to Deepak Gupta)Morning Deepak,
That's brilliant, works beautifully! It's a little funny when it encounters a suppressed part in the feature tree, say in a configuration, but I recon an IF/return statement should fix that. Again thank you so much for the help!
Mark.
-
Re: macro to export center of mass/gravity of parts from an assembly to excel
Deepak Gupta Dec 14, 2012 6:06 AM (in response to Mark Thompson)Add
On Error Resume Next
above
vComps = swAssy.GetComponents(False)
-
Re: macro to export center of mass/gravity of parts from an assembly to excel
Greg Rupp Jan 21, 2013 3:31 PM (in response to Deepak Gupta)This is great Deepak. Thank you for sharing.
One question; I'm trying to find a way to indicate what level a component is. For example, my bom looks like this...
1st lvl sub
2nd lvl sub
3rd lvl sub
4th lvl comp1
4th lvl comp2
4th lvl comp3
3rd lvl sub
4th lvl comp1
4th lvl comp2
4th lvl comp3
...
You get the point. I'd like to be able to indicate the levels each part is on so I can better understand how the Mass property is rolling up the total.
Do you have any suggestion on how this could be done?
-
Re: macro to export center of mass/gravity of parts from an assembly to excel
Deepak Gupta Jan 21, 2013 11:07 PM (in response to Greg Rupp)I'm sure it can be done by modifying the codes. Need some API guru to do (I'll alo give it a try once I'm back home from SolidWorks World)
-
-
Re: macro to export center of mass/gravity of parts from an assembly to excel
Mark Thompson Feb 22, 2013 7:51 AM (in response to Deepak Gupta)Afternoon Deepak,
Apologies for th delay in my response to you last post, I was pulled off this design study onto another project until recently.
In any case, thank you for walking me though the macro so far, I'm almost there! Your macro works beautifully except for detailing each part CoG relative to its part origin rather than top level assembly origin its detailed from. So close! Is there any way to address this?
Again thank you in advance, it really is appreciated considering the size of assemblies and number of load take downs i do day to day.
Best,
Mark.
-
Re: macro to export center of mass/gravity of parts from an assembly to excel
Deepak Gupta Feb 27, 2013 8:04 AM (in response to Mark Thompson)AFAIR the macro was supposed to give details w.r.t. assembly origin. Let me check/test it again.
-
Re: macro to export center of mass/gravity of parts from an assembly to excel
Mark Thompson Feb 27, 2013 4:01 PM (in response to Deepak Gupta)Great, I'd appreciate it.
Mark.
-
Re: macro to export center of mass/gravity of parts from an assembly to excel
Deepak Gupta Mar 3, 2013 6:47 AM (in response to Mark Thompson)Mark, I apologizes for missing the detail that the macro work for detailing each part CoG relative to its part origin rather than top level assembly origin its detailed from.
Let me see if I can fix it to work with Assembly origin.
-
Re: macro to export center of mass/gravity of parts from an assembly to excel
Deepak Gupta Mar 4, 2013 7:22 AM (in response to Mark Thompson)Here is modified version of macro by Jeremiah which work for sub-assembly also. You might need to reset the reference libraries. Also this macro will create the excel file on desktop (which can be changed and set to assembly location folder). The macro keep the excel file visible and also check if the active document is an assembly or not.
-
Re: macro to export center of mass/gravity of parts from an assembly to excel
Mark Thompson Mar 21, 2013 9:19 AM (in response to Deepak Gupta)Afternoon Deepak,
Fantastic, I can't thank you enough, works beutifully.
Best,
Mark.
-
Re: macro to export center of mass/gravity of parts from an assembly to excel
Rahul Patel Aug 6, 2013 12:32 AM (in response to Deepak Gupta)Hi Deepak
I tried and it works more than i expected.
As this macro works for mass, like wise i am looking for file properties of asm/part/drawing.
It will be your gratitude if you have time and its possible at your end to modify for me.
Thanks,
Ankit
-
Re: macro to export center of mass/gravity of parts from an assembly to excel
R. N Aug 22, 2013 9:25 AM (in response to Deepak Gupta)Hi Deepak,
Im competly new this VBA in Solidworks and came across your code for exporting CoG & Mass in assembly - which is exactly what im looking for!
However it works for assemblies and parts in the top assmeblies but not for sub assembly and their parts it calls up the assembly mass and CoG not the part. Any advice on how to fix this?
Im also looking to add the assigned material name to the exported excel, any guidance on this too?
Thank you for help or guidance you can give
Rebecca
-
Re: macro to export center of mass/gravity of parts from an assembly to excel
Deepak Gupta Aug 22, 2013 1:58 PM (in response to R. N)Welcome to SolidWorks forums Rebecca.
I believe that adding a traverse for looking into child part should do the job. For material check GetMaterialPropertyName2
You might also find this post helpful for your future reference.
-
-
Re: macro to export center of mass/gravity of parts from an assembly to excel
John Lega Nov 1, 2013 10:06 AM (in response to Deepak Gupta)This is just what I need but I have one issue. If there are multiple instances (more than one copy) of a part/assembly it reports the same CG for both. I think it determines the first instance CG and just copies it to subsequent instances. Is there a way to get the CG for each instance? For example I have (4) assemblies of "A" but they are located in various locations in the main assembly, yet the excel report shows the same CG for all copies of part "A"..
Example:
Component X Loc (mm) Y Loc (mm) Z Loc (mm) Mass (lbs) Type 00548-12B-100-001-2 9.65E-06 1.64541E-12 3.98E-13 1079.585311 Part 00548-12B-100-004-11 -2193.9885 -416.71875 -965.2635 117.3835157 Part 00548-12B-100-002-3 -1294.98902 -1772.546137 0.000925054 492.4884709 Part 00548-12B-100-003-2 -1360.399555 965.6117546 -4.34657E-12 477.7575023 Part 00548-12B-100-004-10 -2193.9885 -416.71875 965.2635 117.3835157 Part 00548-12B-100-009-1 -1184.451572 -1664.49375 2.6093E-12 1973.586807 Part Thanks so much!
John
-
Re: macro to export center of mass/gravity of parts from an assembly to excel
Chris Jordan Jul 24, 2014 3:55 PM (in response to John Lega)John,
Were you ever able to figure out how to get the CG for each instance?
Thank you for your time,
Chris
-
-
Re: macro to export center of mass/gravity of parts from an assembly to excel
Niels Eskelund Oct 9, 2015 2:34 AM (in response to Deepak Gupta)I just found this thread, and wanted to say Thanx it takes care of my problem.
Niels CHr.
-
-
-
-
-
-
-
Re: macro to export center of mass/gravity of parts from an assembly to excel
Chris Jordan Jul 24, 2014 2:51 PM (in response to Deepak Gupta)This macro works very well to get the COG data relative to the origin coordinate system of the assembly, but I would like to be able to get this data relative to another coordinate system defined within the assembly. How can this be done?
Thank you for your time,
Chris
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
Re: macro to export center of mass/gravity of parts from an assembly to excel
Fx Duverger Apr 10, 2017 10:32 AM (in response to Deepak Gupta)Hi Deepak,
I know you are great on this macro, so could you help me to debug it.
In final I have the same value for all my assemblies.
Why?
Thanks in advance for your response.
PS: my macro and screenshoot are on bottom of this discussion
-
Re: macro to export center of mass/gravity of parts from an assembly to excel
Deepak Gupta Apr 11, 2017 5:24 AM (in response to Fx Duverger)Duverger, the values you're getting for assemblies is from top level assembly and hence same for all assembly.
Check/try macro from this post: https://forum.solidworks.com/message/346767#comment-346767
-
Re: macro to export center of mass/gravity of parts from an assembly to excel
Fx Duverger Apr 11, 2017 7:05 AM (in response to Deepak Gupta)Hi Deepak, thanks a lot to respond i'm desesperate.
I tried your lastest macro named: Export Part Mass and COM (Assy).swp
And I get the same problem.Moreover I tried the last Macro edited here and I've really bad results with lot of 0 on my part.
So you right macro take top assembly.
i'm sorry to disturb you but my final job could be here if you can help me ^^'
@Deepak Gupta-
Re: macro to export center of mass/gravity of parts from an assembly to excel
Deepak Gupta Apr 11, 2017 1:16 PM (in response to Fx Duverger)Try the attached macro. Have not tested much but looks like it is working. I'm still not able to figure out as how to use SetCoordinateSystem in order to specify the CS to be used during calculating mass properties.
Please backup your files and try on sample files first.
-
Re: macro to export center of mass/gravity of parts from an assembly to excel
Fx Duverger Apr 12, 2017 3:13 AM (in response to Deepak Gupta)Hello ^^,
as u can see....
I'll try to modify.
Let me aware to know if you have a new macro....Deepak Gupta
-
Re: macro to export center of mass/gravity of parts from an assembly to excel
Deepak Gupta Apr 12, 2017 6:05 AM (in response to Fx Duverger)Works for me. Can you attach your file or email to me?
-
Export Mass and COM.mp4 3.2 MB
-
Re: macro to export center of mass/gravity of parts from an assembly to excel
Fx Duverger Apr 12, 2017 8:01 AM (in response to Deepak Gupta)Sorry to disturb you,
I can't send you my project.... our project is only internal.
And when I saw your results, your parts get the same coordinate system, or it should not...Deepak Gupta
-
Re: macro to export center of mass/gravity of parts from an assembly to excel
Deepak Gupta Apr 12, 2017 8:14 AM (in response to Fx Duverger)Fx Duverger wrote:
Sorry to disturb you,
I can't send you my project.... our project is only internal.
No worries. Can you make sample files and send over.
And when I saw your results, your parts get the same coordinate system, or it should not...Deepak Gupta
The macro extract the COM based on component origin and not assembly or sub assembly. So basically the results are when you open the component and check COM. Hope this helps.
-
-
-
-
-
-
-
-
-
Re: macro to export center of mass/gravity of parts from an assembly to excel
Jeremiah Davis Dec 13, 2012 3:04 PM (in response to Mark Thompson)Try this, but make sure to add Excel in as a resource. This is specifically designed to output the COM to an excel file for each part component in the assembly (not sub assemblies, but all the way down to the part level).
Dim swApp As SldWorks.SldWorks
Dim SwModel As SldWorks.ModelDoc2
Dim swModExt As SldWorks.ModelDocExtension
Dim swAssembly As SldWorks.AssemblyDoc
Dim SwComp As SldWorks.Component2
Dim MassProp As SldWorks.MassPropertyDim Component As Variant
Dim Components As Variant
Dim Bodies As Variant
Dim BodyInfo As Variant
Dim CenOfM As Variant
Dim RetBool As Boolean
Dim RetVal As LongDim xlApp As Excel.Application
Dim xlWorkBooks As Excel.Workbooks
Dim xlBook As Excel.Workbook
Dim xlsheet As Excel.WorksheetDim OutputPath As String
Dim OutputFN As String
Dim xlCurRow As IntegerSub main()
Set swApp = Application.SldWorks
Set SwModel = swApp.ActiveDoc
Set swAssembly = SwModel
Set swModExt = SwModel.Extension
Set MassProp = swModExt.CreateMassPropertyOutputPath = Environ("USERPROFILE") & "\Desktop\CenterOfMass\"
OutputFN = SwModel.GetTitle & ".xlsx"If Dir(OutputPath & OutputFN) <> "" Then
Kill OutputPath & OutputFN
End IfSet xlApp = Excel.Application
Set xlWorkBooks = Excel.Workbooks
Set xlBook = xlWorkBooks.Add()
Set xlsheet = xlBook.Worksheets("Sheet1")xlsheet.Range("A1").Value = "Component"
xlsheet.Range("B1").Value = "X Loc (mm)"
xlsheet.Range("C1").Value = "Y Loc (mm)"
xlsheet.Range("D1").Value = "Z Loc (mm)"
xlsheet.Range("E1").Value = "Mass (kg)"
xlsheet.Range("F1").Value = "Type"
xlBook.SaveAs OutputPath & OutputFNxlCurRow = 2
RetVal = swAssembly.ResolveAllLightWeightComponents(False)
Components = swAssembly.GetComponents(False)
For Each Component In ComponentsSet SwComp = Component
If SwComp.GetSuppression <> 0 Then
If LCase(Right(SwComp.GetPathName, 3)) <> "asm" Then
Bodies = SwComp.GetBodies3(0, BodyInfo)
'MsgBox SwComp.Name
'If Bodies <> Empty Then
RetBool = MassProp.AddBodies(Bodies)
CenOfM = MassProp.CenterOfMass
xlsheet.Range("A" & xlCurRow).Value = SwComp.Name
xlsheet.Range("B" & xlCurRow).Value = CenOfM(0) * 1000
xlsheet.Range("C" & xlCurRow).Value = CenOfM(1) * 1000
xlsheet.Range("D" & xlCurRow).Value = CenOfM(2) * 1000
xlsheet.Range("E" & xlCurRow).Value = MassProp.Mass
If LCase(Right(SwComp.GetPathName, 3)) = "asm" Then
xlsheet.Range("F" & xlCurRow).Value = "Assembly"
ElseIf LCase(Right(SwComp.GetPathName, 3)) = "prt" Then
xlsheet.Range("F" & xlCurRow).Value = "Part"
Else
xlsheet.Range("F" & xlCurRow).Value = "Undetermined"
End If 'Right 3 of file extension
xlCurRow = xlCurRow + 1
xlBook.Save
'End If 'UBound(Bodies) <> -1
End If 'Not an AssemblyEnd If 'swComp.GetSuppression <> 0
Next Component
xlWorkBooks.Close
xlApp.QuitEnd Sub
-
Re: macro to export center of mass/gravity of parts from an assembly to excel
Mark Thompson Dec 14, 2012 6:11 AM (in response to Jeremiah Davis)Morning Jeremiah,
Interesting take on it. I'm guessing
If SwComp.GetSuppression <> 0 Then
takes care of suppressed parts and
If LCase(Right(SwComp.GetPathName, 3)) = "asm" Then
xlsheet.Range("F" & xlCurRow).Value = "Assembly"
ElseIf LCase(Right(SwComp.GetPathName, 3)) = "prt" Then
xlsheet.Range("F" & xlCurRow).Value = "Part"
Else
xlsheet.Range("F" & xlCurRow).Value = "Undetermined"
End If 'Right 3 of file extension
xlCurRow = xlCurRow + 1
xlBook.Save
takes care of sub assembly parts?
The only problem is when I try to run it I get the following debug msg:
However, I'm not sure how to add excel in as a resource other than referencing it...
-
Re: macro to export center of mass/gravity of parts from an assembly to excel
Jeremiah Davis Dec 14, 2012 11:11 AM (in response to Mark Thompson)This has a little bit of dirty code that I probably should have cleaned up before I posted it. The section about the Assembly never really worked because this does not pick up the mass of sub assemblies, there is additional code to specifically exclude them from having their center of mass created in the list.
As for how to add the reference, in the Macro Editor screen, under the Tools menue and References, look for Microsoft Excel ##.0 Type Library, where ##.0 represents the version of Excel installed on the computer. For me, it is 14.0 because I have Office 2010.
-
Re: macro to export center of mass/gravity of parts from an assembly to excel
Jeremiah Davis Dec 14, 2012 11:17 AM (in response to Mark Thompson)Also, you will want to have a folder on your desktop called "CenterOfMass", just like that, no spaces. Either that or change the destination folder in the macro for the variable OutputPath.
-
Re: macro to export center of mass/gravity of parts from an assembly to excel
Mark Thompson Feb 22, 2013 7:22 AM (in response to Jeremiah Davis)Afternoon Jeremiah,
Apologies for my delayed response to your last post, I was pulled onto another project at the tail end of last year and it's taken me until now to issue the final details etc.
Picking up from where we left off, I'm still rather baffled as to which lines of code I should edit/remove to detail each part name, mass and CoG down through an assembly/sub assemblies without the previous debug error.
Depak's macro worked beautifully except for detailing each part CoG relative to its part origin rather than top level assembly origin. So close!
A little new to this so you may have to point me in the right direction. Again thank you in advance, this is very much appreciated!
Best,
Mark.
-
-
-
Re: macro to export center of mass/gravity of parts from an assembly to excel
Deepak Gupta Nov 3, 2015 11:22 AM (in response to Steve Reinisch)Steve, which macro you're using? There are few on this post; so if you can upload OR point to the one you're using, I can test and fix that.
-
Re: macro to export center of mass/gravity of parts from an assembly to excel
Steve Reinisch Nov 3, 2015 12:04 PM (in response to Deepak Gupta)Thanks Deepak,
I am using the one from the post on 11 June 2014 below.
-
Re: macro to export center of mass/gravity of parts from an assembly to excel
Deepak Gupta Nov 3, 2015 3:23 PM (in response to Steve Reinisch)The macro works fine for me on 2014 and 2015. So make sure there are no missing references and your are pointing to correct versions of SOLIDWORKS and Excel.
Update/correct the macro reference as suggested here (don't worry on the fact that video is for fixing missing library error but check the process to update your macro library): Fix-Update SOLIDWORKS Macro References
-
-
-
-
-
-
Re: macro to export center of mass/gravity of parts from an assembly to excel
Rob Kettenring Jun 10, 2014 2:59 PM (in response to Mark Thompson)Hello;
VB newbie here.
This works great!!!!
Can someone help me to change the units from metric to inch/lbs?
Thanks in advance for your time!
-
Re: macro to export center of mass/gravity of parts from an assembly to excel
Deepak Gupta Jun 10, 2014 3:52 PM (in response to Rob Kettenring)Welcome to SOLIDWORKS forums Rob.
Which of the macro you are using OR wants to change units
You might also find this post helpful for your future reference.
-
Re: macro to export center of mass/gravity of parts from an assembly to excel
Rob Kettenring Jun 10, 2014 4:39 PM (in response to Deepak Gupta)Thanks for the quick response Deepak!
I am using this macro to export center of mass/gravity of parts from an assembly to excel.
All of the data it spits out is in metric. I would like to tweek it to show data in inch pounds.
Also I noticed that the data it generates has all of the assemblies weighing the same
-
Re: macro to export center of mass/gravity of parts from an assembly to excel
Deepak Gupta Jun 11, 2014 10:07 AM (in response to Rob Kettenring)Rob, try this one. Macro is updated for SW2014 so in case you've lower version then you'll have to fix the library references.
Also I noticed that the data it generates has all of the assemblies weighing the same
The macro is pulling information from the top level assembly and using it for the sub-assemblies, not sure why.
-
Re: macro to export center of mass/gravity of parts from an assembly to excel
Rob Kettenring Jun 11, 2014 11:01 AM (in response to Deepak Gupta)Thank you soooo much Deepak! I generate Weight & Balance documents for the FAA, on the equipment we install into aircraft. I am trying to automate that task by pulling out the CG data from the mass properties. Do you know if anybody has figured out why it is pulling information from the top level assembly and using it for the sub-assemblies? Or a work-around?
Thanks in advance for your time!
-
Re: macro to export center of mass/gravity of parts from an assembly to excel
Deepak Gupta Jun 11, 2014 11:31 AM (in response to Rob Kettenring)I've checked the macro quickly to find the issue but didn't see anything yet. I'll debug it later to fix it.
-
Re: macro to export center of mass/gravity of parts from an assembly to excel
Endi Tollkuci Jun 2, 2015 11:13 AM (in response to Deepak Gupta)Hey Deepak,
I am new to the macro scene but trying to find my niche. Was there ever a work around found for the mass properties of the sub-assemblies? I restructured the macro to go to the .prt level of each component however on larger assemblies its taking a good chunk of time to generate the excel file.
I'm not sure if it's as easy as adding an if else statement to filter the .prt and .asm files however the massprop.mass doesn't seem to distinguish between file types. Something like this?
If LCase(Right(SwComp.GetPathName, 3)) = "asm" Then
xlsheet.Range("E" & xlCurRow).value = MassProp.Mass <---- Just not sure what command to use at this point?
If LCase(Right(SwComp.GetPathName, 3)) = "prt" Then
xlsheet.Range("E" & xlCurRow).value = MassProp.Mass
Any amount of guidance or help would be greatly appreciated.
Thanks,
Endi
-
Re: macro to export center of mass/gravity of parts from an assembly to excel
Deepak Gupta Jun 2, 2015 1:16 PM (in response to Endi Tollkuci)Seems OK. Make sure to include end if.
Further add the multiplication factor to get the values in correct units.
-
Re: macro to export center of mass/gravity of parts from an assembly to excel
Endi Tollkuci Jun 2, 2015 1:22 PM (in response to Deepak Gupta)Deepak,
I suppose the only problem is that the MassProp.Mass portion for the .asm gives the entire assemblies mass not the sub-assemblies.
Example being, lets say the total mass of the assembly is 50 and the sub-assembly is 20... the code outputs the sub-assemblies weight as 50 not the 20 even with the if statement I mentioned above. I don't know how to let the code know to use the 20 not the 50.
-
Re: macro to export center of mass/gravity of parts from an assembly to excel
Deepak Gupta Jun 2, 2015 1:30 PM (in response to Endi Tollkuci)Will see if I can tweak it but can't promise anything soon.
-
Re: macro to export center of mass/gravity of parts from an assembly to excel
Endi Tollkuci Jun 2, 2015 1:34 PM (in response to Deepak Gupta)Sounds great!
Thanks again for your time.
-
-
-
-
-
-
-
Re: macro to export center of mass/gravity of parts from an assembly to excel
Brad Moon Mar 10, 2017 12:35 PM (in response to Deepak Gupta)Deepak, I modified your macro to include the file type that Jeremiah had in his (the macro you attached here, I believe).
I also made both macros output the same units (m, kg). In doing so I found that the coordinates and mass were different. I haven't been able to identify why. Your macro matched the output from selecting Mass Properties in SW, and is significantly faster.
I also haven't experienced the problem that Endi has. The output has all the files, and the values coincide with the output within the SW UI.
It's interesting that after so many years SW still doesn't have the ability to output COM natively.
Is there a way to specify a reference coordinate system to output the COM in? This has been asked in this thread already, but there hasn't been a response.
Thanks for your continued help over the years (c.2008).
-
-
-
-
-
Re: macro to export center of mass/gravity of parts from an assembly to excel
Teng Yong Christopher Khoo Jul 5, 2015 3:03 AM (in response to Mark Thompson)Not sure whether the issue of the sub assembly's mass using the assembly's mass.was cleared or not but I used the macro "Macro to Export Part Mass and COG (Assy).swp" written by Deepak to solve it.
Just put in the codes between @@@ as shown below.
' If Bodies <> Empty Then
' @@@
' Mass for asm and prt
Dim swCompModel As SldWorks.ModelDoc2
Set swCompModel = swComp.GetModelDoc2
Dim swMassProps As SldWorks.MassProperty
If Not swCompModel Is Nothing Then
Set swMassProps = swCompModel.Extension.CreateMassProperty()
Else
swApp.SendMsgToUser2 "Error getting the File " & swCompModel.GetName, swMbWarning, swMbOk
End If
' @@@
'
'
'
And change the mass variable callout to swMassProps.Mass instead of MassProp.Mass
xlsheet.Range("E" & xlCurRow).Value = swMassProps.Mass
Extra note:
Please clean up the unused variable or reorder the variable initialization to you want to. Explore Deepak's macro for more depth.
My understanding was that the code below does not work with sub assembly. That is why the mass was just taken from the parent assembly instead.
RetBool = MassProp.AddBodies(Bodies)
Therefore you need the code below inside the "For Each Component In Components" loop
Set swMassProps = swCompModel.Extension.CreateMassProperty()
instead of the code in the parent assembly
Set swModExt = SwModel.Extension
Set MassProp = swModExt.CreateMassProperty
-
Re: macro to export center of mass/gravity of parts from an assembly to excel
Chiheb Gouta Apr 11, 2016 7:34 AM (in response to Mark Thompson)Experts,
I am working on mass and cog extraction and exportation to excel
I am working on vb.net and not vba. I have implemeted the mecro posted by Deepak (Deepak Gupta 4 mars 2013 07:22) on vb.net bu and here is the code:Public Sub ComponentsAttributs()
swModelDoc2 = iSwApp.ActiveDoc
'Dim swModExt As ModelDocExtension
Dim swAssembly As AssemblyDoc = swModelDoc2
Dim SwComp As Component2
Dim MassProp As MassProperty
Dim Component As Object
Dim Components As Object
Dim Bodies As Object
'Dim BodyInfo As Object
Dim CenOfM As Object
Dim RetBool As Boolean
Dim RetVal As Long
'Dim OutputPath As String
'Dim OutputFN As String
Dim ExcelCurRow As Integer
'--------------------------------------------------'
MassProp = swModelDoc2.Extension.CreateMassProperty
'Start Excel and get Application object.
ExcelApp = CreateObject("Excel.Application")
ExcelApp.Visible = True
' Add a new workbook.
ExcelWorkBook = ExcelApp.Workbooks.Add
ExcelSheet = ExcelWorkBook.ActiveSheet
ExcelSheet.Cells(1, 1).Value = "Component"
ExcelSheet.Cells(1, 2).Value = "X Loc (mm)"
ExcelSheet.Cells(1, 3).Value = "Y Loc (mm)"
ExcelSheet.Cells(1, 4).Value = "Z Loc (mm)"
ExcelSheet.Cells(1, 5).Value = "Mass (kg)"
ExcelSheet.Cells(1, 6).Value = "Type"
ExcelCurRow = 2
RetVal = swAssembly.ResolveAllLightWeightComponents(False)
Components = swAssembly.GetComponents(False)
For Each Component In Components
SwComp = Component
If SwComp.GetSuppression <> 0 Then
'If LCase(Right(SwComp.GetPathName, 3)) <> "asm" Then
Bodies = SwComp.GetBodies3(-1, 1)
'MsgBox SwComp.Name
'If Bodies <> Empty Then
RetBool = MassProp.AddBodies(Bodies)
CenOfM = MassProp.CenterOfMass
txtComponentsData.Text = txtComponentsData.Text & SwComp.Name & vbCrLf
ExcelSheet.Range("A" & ExcelCurRow).Value = SwComp.Name
ExcelSheet.Range("B" & ExcelCurRow).Value = CenOfM(0) * 1000
ExcelSheet.Range("C" & ExcelCurRow).Value = CenOfM(1) * 1000
ExcelSheet.Range("D" & ExcelCurRow).Value = CenOfM(2) * 1000
ExcelSheet.Range("E" & ExcelCurRow).Value = MassProp.Mass
If LCase(Microsoft.VisualBasic.Right(SwComp.GetPathName, 3)) = "asm" Then
ExcelSheet.Range("F" & ExcelCurRow).Value = "Assembly"
ElseIf LCase(Microsoft.VisualBasic.Right(SwComp.GetPathName, 3)) = "prt" Then
ExcelSheet.Range("F" & ExcelCurRow).Value = "Part"
Else
ExcelSheet.Range("F" & ExcelCurRow).Value = "Undetermined"
End If 'Right 3 of file extension
ExcelCurRow = ExcelCurRow + 1
'End If 'UBound(Bodies) <> -1
'End If 'Not an Assembly
End If 'swComp.GetSuppression <> 0
Next Component
ExcelSheet.UsedRange.EntireColumn.AutoFit()
End Sub
the code goes on but I have the following result: the same values on all compoenents.
Can anyone help me?-
Re: macro to export center of mass/gravity of parts from an assembly to excel
Chiheb Gouta Apr 11, 2016 10:04 AM (in response to Chiheb Gouta)Problem Solved!
-
Re: macro to export center of mass/gravity of parts from an assembly to excel
Fx Duverger Apr 10, 2017 10:27 AM (in response to Chiheb Gouta)Hi Chiheb Gouta,
how you made to have not the same values between assemblies?
-
-
-
Re: macro to export center of mass/gravity of parts from an assembly to excel
Fx Duverger Apr 10, 2017 10:27 AM (in response to Mark Thompson)Dear all,
I'm currently using this macro but the result is the same for all assembly.
Someone could help me to debug it....Follow the macro used:
Dim swApp As SldWorks.SldWorks
Dim SwModel As SldWorks.ModelDoc2
Dim swModExt As SldWorks.ModelDocExtension
Dim swAssembly As SldWorks.AssemblyDoc
Dim SwComp As SldWorks.Component2
Dim MassProp As SldWorks.MassProperty
Dim swMassProps As SldWorks.MassProperty
Dim Component As Variant
Dim Components As Variant
Dim Bodies As Variant
Dim BodyInfo As Variant
Dim CenOfM As Variant
Dim RetBool As Boolean
Dim RetVal As Long
Dim xlApp As Excel.Application
Dim xlWorkBooks As Excel.Workbooks
Dim xlBook As Excel.Workbook
Dim xlsheet As Excel.Worksheet
Dim OutputPath As String
Dim OutputFN As String
Dim xlCurRow As Integer
Sub main()
Set swApp = Application.SldWorks
Set SwModel = swApp.ActiveDoc
If SwModel Is Nothing Then
swApp.SendMsgToUser2 "An assembly must be an active document.", swMbWarning, swMbOk
Exit Sub
End If
If SwModel.GetType <> swDocASSEMBLY Then
swApp.SendMsgToUser2 "An assembly must be an active document.", swMbWarning, swMbOk
Exit Sub
Else
Set swAssembly = SwModel
End If
OutputPath = Environ("USERPROFILE") & "\Desktop\"
OutputFN = SwModel.GetTitle & ".xlsx"
If Dir(OutputPath & OutputFN) <> "" Then
Kill OutputPath & OutputFN
End If
Set xlApp = Excel.Application
xlApp.Visible = True
Set xlWorkBooks = Excel.Workbooks
Set xlBook = xlWorkBooks.Add()
Set xlsheet = xlBook.Worksheets("Feuil1")
xlsheet.Range("A1").Value = "Component"
xlsheet.Range("B1").Value = "X Loc (mm)"
xlsheet.Range("C1").Value = "Y Loc (mm)"
xlsheet.Range("D1").Value = "Z Loc (mm)"
xlsheet.Range("E1").Value = "Mass (kg)"
xlsheet.Range("F1").Value = "Type"
xlBook.SaveAs OutputPath & OutputFN
xlCurRow = 2
RetVal = swAssembly.ResolveAllLightWeightComponents(False)
Components = swAssembly.GetComponents(False)
For Each Component In Components
Set SwComp = Component
If SwComp.GetSuppression <> 0 Then
'If LCase(Right(SwComp.GetPathName, 3)) <> "asm" Then
Bodies = SwComp.GetBodies2(0)
'MsgBox SwComp.Name
'If Bodies <> Empty Then
Set swModExt = SwModel.Extension
Set MassProp = swModExt.CreateMassProperty
MassProp.UseSystemUnits = True
'Mass for asm and prt
Dim swCompModel As SldWorks.ModelDoc2
Set swCompModel = SwComp.GetModelDoc2
Dim swMassProps As SldWorks.MassProperty
If Not swCompModel Is Nothing Then
Set swMassProps = swCompModel.Extension.CreateMassProperty()
Else
swApp.SendMsgToUser2 "Error getting the File " & swCompModel.GetName, swMbWarning, swMbOk
End If
RetBool = MassProp.AddBodies(Bodies)
CenOfM = MassProp.CenterOfMass
xlsheet.Range("A" & xlCurRow).Value = SwComp.Name
xlsheet.Range("B" & xlCurRow).Value = CenOfM(0) * 1000
xlsheet.Range("C" & xlCurRow).Value = CenOfM(1) * 1000
xlsheet.Range("D" & xlCurRow).Value = CenOfM(2) * 1000
xlsheet.Range("E" & xlCurRow).Value = swMassProps.Mass
If LCase(Right(SwComp.GetPathName, 3)) = "asm" Then
xlsheet.Range("F" & xlCurRow).Value = "Assembly"
ElseIf LCase(Right(SwComp.GetPathName, 3)) = "prt" Then
xlsheet.Range("F" & xlCurRow).Value = "Part"
Else
xlsheet.Range("F" & xlCurRow).Value = "Undetermined"
End If 'Right 3 of file extension
xlCurRow = xlCurRow + 1
Erase CenOfM
'End If 'UBound(Bodies) <> -1
'End If 'Not an Assembly
End If 'swComp.GetSuppression <> 0
Next Component
xlsheet.UsedRange.EntireColumn.AutoFit
xlBook.Save
'xlWorkBooks.Close
'xlApp.Quit
End Sub
-
Re: macro to export center of mass/gravity of parts from an assembly to excel
Fx Duverger Apr 13, 2017 3:23 AM (in response to Mark Thompson)Hello all,
After collaboration with Deepak Gupta, we get this macro (CF).
Problem is than she give data in local, but we want to get them in global system as you have in your mass proprieties window.If you know or look for on it share ;p








