40 Replies Latest reply on May 8, 2014 12:54 PM by Anthony Bear

    display CoG in drawing

    Mark Greenwell

      Hi

       

      Does anyone know if SolidWorks 2010 allows you to place a Center of Gravity marker into a drawing view, this position taken directly from a part or assembly.

       

      Thanks

       

      Mark (SolidWorks 2009 SP4.1)

        • Re: display CoG in drawing
          Wayne Tiffany

          I don't know the answer to your direct question.  What we do here is run a macro in the model file that will put a sketch point at the CG.  Then in the drawing you can use that point.

           

          WT

          • Re: display CoG in drawing
            Devon Sowell

            Hello Mark-

             

            I just posted how to do this on my SolidWorks blog: http://designsmarter.typepad.com/devonsowell/

             

            Get the coordinates for the center of mass and create a 3D sketch point.

             

            Devon Sowell
            http://www.3-ddesignsolutions.com

              • Re: display CoG in drawing
                Mark Greenwell

                Hi Devon / Wayne,

                 

                I have a macro that palces a CoG pont in a part sketch, but not an Assembly, i had thought about a 3D point taken from assembly info, but does this update if the assembly changes.

                I would have thought by now that the ability to place a CoG into a drawing from a part or assembly would be a standard function. SolidWorks cometitors have this function staight from the box.

                 

                Ah well maybe in 2011

                 

                Thanks

                 

                Mark

                  • Re: display CoG in drawing
                    Wayne Tiffany

                    The macros I posted will work in an assy as well.  Unfortunately neither one will produce something that changes dynamically as the model changes.  You have to run it again yourself.

                     

                    WT

                    • Re: display CoG in drawing
                      Jim Sculley

                      I have what I believe is a working macro that inserts a point at the CG and has a macro feature to keep it up to date when the model is rebuilt.  I'm stuck on what should be very simple.  I cannot figure out how to edit an existing 3D sketch and then exit that sketch via the API.  This should be simple, but it is eating my lunch.

                       

                      Jim S.

                        • Re: display CoG in drawing
                          Jim Sculley

                          Problem solved.  Give me a few minutes to clean it up and I'll post the code.

                           

                          Jim S.

                            • Re: display CoG in drawing
                              Jim Sculley

                              Give it a whirl.  Parts and assemblies.  For the macro feature to work, the Module Name in the VBA project MUST match the String in the methods array as noted in the commented code below.  Watch out for word wrapping on the call to 'InsertMacroFeature3'.

                               

                              ======================

                              Dim swApp As SldWorks.SldWorks
                              Dim mDoc As ModelDoc2
                              Dim ext As ModelDocExtension
                              Dim boolStatus As Boolean
                              Sub Main()
                                  Set swApp = Application.SldWorks
                                  Set mDoc = swApp.ActiveDoc
                                  Set ext = mDoc.Extension
                                  createCGSketch
                                  Set sketchFeat = mDoc.FeatureByPositionReverse(0)
                                  sketchFeat.Name = "CGSketch"
                                  Set macroFeat = createMacroFeature
                                  boolStatus = macroFeat.MakeSubFeature(sketchFeat) 'absorb Sketch into MacroFeature
                              End Sub

                              Function getCG() As Variant
                                  Dim props As MassProperty
                                  Set props = ext.CreateMassProperty
                                  Dim vCG As Variant
                                  vCG = props.CenterOfMass
                                  getCG = vCG
                              End Function

                              Private Sub createCGSketch()
                                  Dim sketchMgr As SketchManager
                                  Dim currentSketch As Sketch
                                  Dim sketchPt As SketchPoint
                                  Dim sketchFeat As Feature
                                  Dim cgCoords As Variant
                                  cgCoords = getCG()
                                  Set sketchMgr = mDoc.SketchManager
                                  sketchMgr.Insert3DSketch (False)
                                  Set sketchPt = sketchMgr.CreatePoint(cgCoords(0), cgCoords(1), cgCoords(2))
                                  Set currentSketch = sketchMgr.activeSketch
                                  sketchMgr.Insert3DSketch (True)
                              End Sub

                              Private Sub updateCGSketch()
                                  Dim newCG As Variant
                                  Dim pDoc As PartDoc
                                  Dim assyDoc As AssemblyDoc
                                  Dim sketchFeature As Feature
                                  If mDoc.GetType = swDocPART Then
                                      Set pDoc = mDoc
                                      Set sketchFeature = pDoc.FeatureByName("CGSketch")
                                  Else
                                      Set assyDoc = mDoc
                                      Set sketchFeature = assyDoc.FeatureByName("CGSketch")
                                  End If
                                  Dim cgSketch As Sketch
                                  Dim points As Variant
                                  Dim point As SketchPoint
                                  newCG = getCG()
                                  Set cgSketch = sketchFeature.GetSpecificFeature2
                                  points = cgSketch.GetSketchPoints2()
                                  Set point = points(0)
                                  boolStatus = point.SetCoords(newCG(0), newCG(1), newCG(2))
                              End Sub

                              Private Function createMacroFeature() As Feature
                                  Dim fm As FeatureManager
                                  Dim Methods(8) As String
                                  Dim macroFile As String
                                  Set fm = mDoc.FeatureManager
                                  macroFile = swApp.GetCurrentMacroPathName
                                  '  The VBA Module name MUST match the String in array elements 1 and 4 below.
                                  Methods(0) = macroFile: Methods(1) = "CenterOfGravity": Methods(2) = "swmRebuild"
                                  Methods(3) = macroFile: Methods(4) = "CenterOfGravity": Methods(5) = "swmEditDefinition"
                                  Methods(6) = "": Methods(7) = "": Methods(8) = "" ' Security settings. See 'InsertMacroFeature3' API docs
                                  Dim Names As Variant        'N/A
                                  Dim Types As Variant        'N/A
                                  Dim Values As Variant       'N/A
                                  Dim dimTypes As Variant     'N/A
                                  Dim dimValue As Variant     'N/A
                                  Dim icons(2) As Variant     'Create some icons if you like.  See 'InsertMacroFeature3' API docs
                                  Dim vEditBodies As Variant  'N/A
                                  Set createMacroFeature = fm.InsertMacroFeature3("CenterOfGravity", "", (Methods), Names, Types, Values, dimTypes, dimValues, vEditBodies, (icons), swMacroFeatureByDefault)
                              End Function

                              'Called automatically when you attempt to edit the definition of the Macro Feature
                              'The CG calculation is automatic, so editing the definition is not necessary, so we return 'False'
                              Function swmEditDefinition(vApp As Variant, vModel As Variant, vFeature As Variant) As Variant
                                  Dim sw As SldWorks.SldWorks
                                  Dim mDoc As ModelDoc2
                                  Dim feat As Feature
                                  Set sw = vApp
                                  Set mDoc = vModel
                                  Set feat = vFeature
                                  swmEdit = False
                              End Function
                              'Called automatically when a rebuild occurs.  We simply call a method to update the CG sketch
                              Function swmRebuild(vApp As Variant, vModel As Variant, vFeature As Variant) As Variant
                                  updateCGSketch
                              End Function
                              ===============================

                               

                              Jim S.

                                • Re: display CoG in drawing
                                  Wayne Tiffany

                                  I did a cut and paste of this code to create a new macro and it looked like it parsed out just fine.  However, when I run it, I get the error you see when I execute the boolStatus = macroFeat.MakeSubFeature(sketchFeat) line.  I put a break in and stepped through and it appears that the macro feature is not being created, although there are no errors generated by that function.

                                   

                                  Is there a reason you didn't just save this out as a macro rather than posting the code?  I checked just to make sure I had 2009 references and I did.  Any thoughts?

                                   

                                    WT

                                    • Re: display CoG in drawing
                                      Jim Sculley

                                      Ack!

                                       


                                      Code cleaning error.  Everything was in one subroutine.  While refactoring into smaller pieces I moved/deleted a couple of lines and forgot to test it one last time before posting.  The error is because there are a couple of Feature variables used without having been declared.  One of them is 'macroFeat', the other is 'sketchFeat'.  A couple of bugs when dealing with assemblies too ( I added those at the last minute).  I think I've got those sorted out.

                                       

                                      Moral of the story: Don't rush to post code before leaving work.

                                       

                                       

                                      Revised macro attached.

                                       


                                      Jim S.

                                        • Re: display CoG in drawing
                                          Wayne Tiffany

                                          Seems to work pretty well!  I renamed it to CGPoint - Dynamic.swp to go along with the other two that I had.  I can see this being a very nice threesome in terms of now the user can do whatever they wish as far as monitoring the CG.

                                           

                                          I tried several things, and one thing I see that may cause a bit of a problem is that the CenterOfGravity1 feature has to be the last in the tree.  If you add more cuts, bosses, etc, you have to remember to drag the CenterOfGravity1 below them.  Otherwise the CG is calculated without considering that new feature.  But I can also see this being used to an advantage.  Not a big problem.

                                           

                                          The other thing I tried is to put the macro somewhere, then run it on a part, save the part, close SW, and move the macro somewhere else.  When I reopened the part, the CenterOfGravity1 errored with the message that it couldn't find it's momma.  That's probably a good way to behave because then the user knows that something is not right.  If need be, then they can just go find it and rerun it to reinsert the feature.

                                           

                                          I would also suggest that you put some verbiage at the top of the macro that states who you are, what this is supposed to do, and a date.  That way it is recorded that you are the creator of this and people will know where to throw their coins.  :-)

                                           

                                          And, one more thought.  Would it make sense to look at the functionality of the two that I posted and try to roll them into this one such that we can get this down to just one macro with choices?  I know there have been times where I wanted to maintain several points as I went along to track the revisions.  That mode would run the macro on demand and insert a new point each time.  I also wonder if there would be a desire to not have the point update until asked for. Again and on-demand run, but would update the current point.  This may be an issue if the part were large and this added too much overhead.  (I don't know how much it will add on a large part - just guessing.)  Therefore those others may still have some merit.  Thoughts?

                                           

                                          Oh, and thanks - I love it!  :-)

                                           

                                            WT

                                            • Re: display CoG in drawing
                                              Wayne Tiffany

                                              Jim,

                                               

                                              Here is the part I was playing with last night.  When I tried it this morning I got errors.  Open the part and unsuppress Extrude2 - see what you get. 

                                               

                                                WT

                                                • Re: display CoG in drawing
                                                  Wayne Tiffany

                                                  I just tried this on another machine (XP32) and I saw the same thing.  I created a part, put in a cut, ran the macro, suppressed the cut above the CG feature, saved the part and closed it.  Then I opened the part and unsuppressed the cut above the CG feature - error generated.

                                                   

                                                  I had not even closed SW or anything else.  Strange indeed.

                                                   

                                                  WT

                                                    • Re: display CoG in drawing
                                                      Jim Sculley

                                                      New 0.5 alpha version attached (yes I made up the version) .

                                                       

                                                      Features:

                                                       

                                                      • Three icons (normal, suppressed, highlighted).  Put them in the same folder as the macro itself.
                                                      • Automatic positioning at end of feature tree.
                                                      • Macro feature code and icons are embedded in the file, so part should be portable.
                                                      • Code refactoring to remove some smelly bits including the bug you described (I hope).
                                                      • A little bit of documentation

                                                       

                                                       

                                                      Jim S.

                                                        • Re: display CoG in drawing
                                                          Wayne Tiffany

                                                          Hey, still up and watching here?  0.5 Alpha tester here........ ;-)

                                                           

                                                          I think I have found a bug, but you'll have to verify it.

                                                           

                                                          I generally start with a basic block part, which is just a rectangular extruded block centered on the system planes.  If I open that and then put in the macro feature, it appears that any feature after that is not accounted for, even though the macro feature goes to the bottom.  Since I know the CG has to be changing as calculated by SW, then my assumption is that either the feature is not rebuilding properly, or it's not updating the sketch.  But if I put in a feature, then run the macro, it appears to work properly, which made me wonder if there needed to be two features before the macro feature.

                                                           

                                                          So then I figured that I would start with an empty part.  Run the macro and then put in the initial extrude.  No good there.  When I rebuild, the CG is not changing to the new CG of the part.

                                                           

                                                          Next up was to start with an empty part, put in the initial extrude, then run the macro.  At that point it goes to the proper CG.  Then put in another feature and the CG appears to update properly.

                                                           

                                                          So it seems that what's required is to insert at least one feature into the part before running the macro.  It doesn't seem to matter if there is one or two total features before the macro, just that I inserted at least one.

                                                           

                                                          But if I open the basic block, add an extrusion, then save and close it, then open it and run the macro, it seems to work.  Hmmm, now I'm a bit confused.

                                                           

                                                          (Some many minutes later.)  Ok, I think I found the key, and all of the above is probably meaningless.  :-(  I think what's happening with my basic block and the macro first in an empty part is that the CG at that point is coincident with the origin.  Remember, my basic block is centered on all three planes - the CG is at the origin.  An empty part has the CG at the origin.  I created a new basic block template that wasn't centered on the planes.  When I opened this one and inserted the macro feature, all seemed to work properly.  Hopefully that will be a clue.

                                                           

                                                          Switching gears here.

                                                           

                                                          I like the icons - cute.

                                                           

                                                          I tried saving the part with the macro feature in it, closing SW, and then hiding the macro and its icons.  (I moved the cheese....)  Upon opening the part, I find that the feature still works - good job.  I did notice that the icon changed to the generic macro icon - the person.  Upon returning the cheese - the icons, but not the macro, I see it picked them up again.  If I bring back only the highlight one, it appears that it doesn't know to use it.  I presume that whatever it uses for the regular one, that's the set it uses for highlight and suppressed.  I don't don't know that this is a problem, only an observation.

                                                           

                                                          Every time I run the macro, the next thing I do is expand it and show the sketch.  I figure that the main reason I am putting this feature in there is to "watch" the CG, therefore I want to "watch" the position of the sketch point.  Would it not make sense to have it showing by default?  Or maybe a user defined variable at the top of the macro such that the CAD admin could set it one way or the other.  Hey, how about flashing pink and purple??

                                                           

                                                          Oh, nice docs.  :-)

                                                           

                                                          (I'm done for the night.........)

                                                           

                                                            WT

                                                            • Re: display CoG in drawing
                                                              Jim Sculley
                                                              I think what's happening with my basic block and the macro first in an empty part is that the CG at that point is coincident with the origin.

                                                              If auto-relations are enabled, a Concident relation may be added if the point lies on something else, such as the Origin.   This prevents the point from updating later.  I've changed the macro to temporarily disable auto-relations if necessary.

                                                               

                                                              I tried saving the part with the macro feature in it, closing SW, and then hiding the macro and its icons.  (I moved the cheese....)  Upon opening the part, I find that the feature still works - good job.  I did notice that the icon changed to the generic macro icon - the person.  Upon returning the cheese - the icons, but not the macro, I see it picked them up again.  If I bring back only the highlight one, it appears that it doesn't know to use it.  I presume that whatever it uses for the regular one, that's the set it uses for highlight and suppressed.  I don't don't know that this is a problem, only an observation.

                                                              That's odd.  When I perform the same test (closing the part and SW, moving the macro and icons, reopening the part), the macro feature and icons are still working fine.  This is on SW2009 SP4.0 Windows XP x32.  Will try Vista x64 tomorrow.

                                                               

                                                              Every time I run the macro, the next thing I do is expand it and show the sketch.  I figure that the main reason I am putting this feature in there is to "watch" the CG, therefore I want to "watch" the position of the sketch point.  Would it not make sense to have it showing by default?

                                                               

                                                              I've done this in the next version.  See attached.

                                                               

                                                              Jim S.

                                                            • Re: display CoG in drawing
                                                              Gabe Biertzer

                                                              Jim,

                                                              This macro is excellent.  I had been using one that functions similarly - parts only.  Having the added capability for handling assemblies is huge!  Thanks for sharing this macro.

                                                               

                                                              It doesn't affect me, but I did notice a limitation.  It will not roll to the end of the tree for sheet metal components in the flattened state.  The "Flat-Pattern" feature seems to trump the "COG" feature for end of tree privileges.  The only time I could see this being an issue is if the COG had to be shown on the flat drawing view.

                                                               

                                                              Well done!

                                                               

                                                              Thanks,

                                                              Gabe

                                                                • Re: display CoG in drawing
                                                                  Jim Sculley
                                                                  It will not roll to the end of the tree for sheet metal components in the flattened state.  The "Flat-Pattern" feature seems to trump the "COG" feature for end of tree privileges.  The only time I could see this being an issue is if the COG had to be shown on the flat drawing view.

                                                                  Yep.  The flat pattern appears to have special status.  An 'Unfold' however does not.  So, if necessary, one could add some unfold features to determine the CG for the flattened geometry.

                                                                   

                                                                  Jim S.

                                                          • Re: display CoG in drawing
                                                            Jim Sculley


                                                            If you add more cuts, bosses, etc, you have to remember to drag the CenterOfGravity1 below them.


                                                            Macro features actually let you control the location of them.  I have tweaked the code to force the CG to the bottom of the tree.

                                                             

                                                            The other thing I tried is to put the macro somewhere, then run it on a part, save the part, close SW, and move the macro somewhere else.


                                                            Again the macro feature lets you control this.  There is the option to embed the macro in the model.  I turned this on for version 2.0

                                                             

                                                            I would also suggest that you put some verbiage at the top of the macro that states who you are, what this is supposed to do, and a date.

                                                             

                                                            Documentation?  BAH!.   

                                                             

                                                            I know there have been times where I wanted to maintain several points as I went along to track the revisions.


                                                            I had hoped that adding CG features at several points along the tree would allow this type of tracking.  My initial tests didn't pan out.  The CG in the middle of the tree didn't want to rebuild unless I rolled back to it.  Not sure if this is my bug, SW bug, or 'working as designed'.

                                                             

                                                            That mode would run the macro on demand and insert a new point each time.

                                                            Seems doable.  You could have auto-update CG points and one-off CG points.  You can actually implement 'Edit Definition' and have a property manager page for it to toggle between the two types.

                                                             

                                                            I've whipped up some icons as well.  I'll include them in the next upload.

                                                             

                                                            Jim S.

                                                              • Re: display CoG in drawing
                                                                Paul Swinney

                                                                     Jim,

                                                                 

                                                                     Thanks mucho, this thing works great.  I just happen to check the forum this a.m. and saw this thread.  Had a part that fit the bill, high RPM spinning part with iregular shaped pockets and a relief feature on the OD to balance the whole body.  Threw the macro in there and gave me an easy/quick way to measure CG -vs- Origin.  When I put two different configs in a dwg I would get a error, something about part maybe corrupt, nothing was wrong in the part.  I'll try to post the part or e-mail to you tomorrow, got the job done fine - message looked bad but the part was fine.  Thanks again.

                                                                • Re: display CoG in drawing
                                                                  Robert Chivers

                                                                  Jim,

                                                                   

                                                                  You are the MAN!!!!, I've been wanting this automatic C of G for years since using Solidedge!!.

                                                                   

                                                                  One further improvement I did was to run the Macro in the template files (.dot) and save them. Now all new parts and assemblies have the automatic C of G as standard!!

                                                                   

                                                                  Good job, what address to I send the beer to?

                                                                   

                                                                   

                                                                   

                                                              • Re: display CoG in drawing
                                                                Mark Greenwell

                                                                Jim,

                                                                 

                                                                Thanks for posting your CoG macro, still cannot belive this function is not available staight out of the box.

                                                                 

                                                                Thanks

                                                                 

                                                                Mark

                                                                • Re: display CoG in drawing
                                                                  Jordan Woods-Wahl

                                                                  Jim,

                                                                   

                                                                  I too have been using the Macro and it has been greats. Thanks again for your contribution.  In running through some assemblies I did find that hidden/bodies components are included with the COG generated by the macro. When that box is unchecked in mass propertiers the macro still includes them. 

                                                                   

                                                                  Is it possible for the macro to follow the preference for hidden bodies set by mass prop?

                                                                   

                                                                  Again, thanks for your contribution.

                                                                   

                                                                  Jordan