Page 1 of 1

Custom mass calculations (release)

Posted: Mon Aug 26, 2024 4:59 am
by laukejas
Hi,

Since SW lacks an option to override mass properties with an equation, I wrote a macro that does just that. If you set it to run on rebuild, it will seamlessly integrate into your model, and recalculate mass based on an equation of your choosing whenever model is rebuilt.

While I wrote this macro to help me predict the weight of 3D printed parts, it is just a framework - you can write your own equation inside the macro with whatever parameters you want, and it will do the job for you.

Image

Why is it useful:

I model a lot of 3D printed parts that are printed with various infill percentage and various number of perimeter lines. Typically, only the slicer software can tell me the predicted weight of the part based on these values, but I need to know it during design stage in SW. I don't want to have dozens of variations of my materials in the material library with different densities for every possible scenario. Instead, I'd rather approximate the weight based on model properties (volume, surface area, etc.), enter the print parameters (infill, perimeter count etc.), and have SW calculate it for me, and have it update automatically as I change the geometry.

This is also useful if you model parts for other manufacturing methods, and for whatever reason the final weight of the part does not match what SW tells you - for example, a part that will be painted, and the paint adds a non-negligible amount of weight.

How to use it:

Open the macro with the editor. If you don't know how to do macros, there are only two things you need to worry about - an equation, and parameters you will be using in that equation. The equation is inside the CalculateMass method. The parameters are listed in the main method. Feel free to rewrite these two bits to whatever you need.

I strongly recommend using the Run Macro on Rebuild macro that will allow you to embed my macro into your model, so it will rebuild on every rebuild. Or if you feel you didn't make your click quota in SOLIDWORKS today, make a button from my macro and click it when needed to update the mass.

Important: the Run Macro on Rebuild that I linked above can either embed a copy of my macro into your model, or just make a link.

My macro can either use the parameter values from the code itself, or make a copy of them to SOLIDWORKS model Equations (as Global Variables).

1) If you want to have one instance of this macro for all your models that share the same equation for mass calculations, but need different parameters, then in my macro, set UseEquations = True. Now the parameter values in the code will be just a template for new parts, and you can set the actual values by modifying these Global Variables.

2) If you want to have a copy of this macro attached to every part (for example if they use different equations, or if you need to transfer your models between computers and want this macro to tag along), then you don't need to use Global Variables, so set UseEquations = False. Now, the parameter values will be taken directly from the code. You can still edit these values conveniently, since the copy of the macro will be accessible through Design Binder (right click on the macro -> Edit)

(Myself, I prefer to use the first method since all my parts can use the same equation and I don't move my models between computers a lot. Besides, you can have several copies of this macro with different equations (for example, "CustomMass-PrintedParts.swp", "CustomMass-PaintedParts.swp", etc.)

Pro tip #1: if you like this macro, integrate it into your model with Run Macro on Rebuild (embed or not, up to you), and save it as a new template. Now all your new parts will use this macro by default.

Pro-tip #2: if you want to override the mass with a percentage of the actual mass (for example, make SW report mass as 105% of the actual mass), I included a method GetModelMass that disregards whatever Mass Properties reports (since that mass may already be overriden by this macro), and instead reports back mass based on density * volume. This works for parts only though.

How to remove it:

If you embedded/linked this macro in your model and want to remove it, simply delete it from the Design Binder, delete the MacroRunner feature at the bottom of the FeatureManager Tree, and finally go into Evaluate-Mass Properties, and uncheck "Override mass".

Technical details about this example:
This part should only interest you if you also 3D print stuff and want to figure out how the equation in my specific example works.

Basically it calculates the infill mass based on infill percentage ("Infill density" parameter), times the surface area, times the material density. However, since in most slicers, different infill type will result in different actual filling percentage, and the math behind it is very complex, I added "Infill weight factor" parameter, which is basically a fudge factor to make the macro prediction match the slicer prediction.
Perimeter mass is calculated using the surface area, the perimeter line width and count, as well as the material density, also with a fudge factor. This does not account for the top/bottom layer count, and basically assumes that top/bottom are as thick as the perimeters. Close enough for most of my parts, but you might want to modify the equation as per your needs.

If the numbers don't match what the slicer predicts for you (after matching the material densities and other properties), then just adjust the "Perimeter weight factor" and "Infill weight factor" until it does. From there on, the equation should give you good predictions, at least for that model.

I know this equation is very rough, but it still gets me to within 1-2% of the weight that slicer predicts, which is good enough for me. If you need more accuracy, feel free to modify that equation.

Technical details on the macro:
If you use the macro with UseEquations = True, then on the first run it will have to create these equations. For whatever reason, this is a bit slow. And it also needs a Force Rebuild afterwards, otherwise model ends up in a weird state and reading Mass Properties doesn't work. If you know how to fix this, let me know. This is only a nuisance for first time running this macro on a new part. If you add it to a template, it won't matter.

Re: Custom mass calculations (release)

Posted: Tue Aug 27, 2024 1:36 pm
by laukejas
I see this macro has been downloaded 56 times now, and I just discovered that for whatever reason it doesn't want to work with Run On Rebuild for me, trying to add it throws an error out of the blue. Apparently CreateMassProperty2 doesn't work when the macro is launched by Run On Rebuild, but works when running it manually... Not sure why. Does it work with Run On Rebuild for you ?