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)
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)
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
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
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.
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.
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
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.
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
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.
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
New 0.5 alpha version attached (yes I made up the version) .
Features:
Jim S.
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
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
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.
All seems to be working properly. I tried it at home on XP32, here on my work machine XP64, and on a Vista 64 machine. I think the auto-relation must have been the issue as once I figured out that things didn't work if the CG was at the origin, you made the change, and now the issue appears to be gone. Good job - thanks.
I can't explain the icon issue. I tried it with this version (0.6 Alpha) at home last night and it seemed to work fine. But you talked like you didn't change anythign there.
I see this version is 0.6 Alpha - will it ever get to play with the big kids and become a full 1.0? :-)
WT
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.
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