Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations KootK on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Volume in Feet

API and Macros

Volume in Feet

by  SBaugh  Posted    (Edited  )
Here is a Macro that lists Volume in feet. See below for details.

We had a customer who wanted a volume on the drawing in feet, but wanted to build his part in inches. You can't find a way to do that in SW.

This macro will insert a macro feature into the feature tree. This macro will then run every rebuild. The macro takes the volume in the natural system units, meters, and converts it into feet. Then it adds a property called volume and adds that value. It updates when the model is changed.

If you add features after the Macro feature they will not be included. You can drag and drop the macro up and down the tree. This does require a ctrl-q rebuild for whatever reason.

It can be found at:

http://www.scottjbaugh.com/Design_Portfolio/Contributed%20SW%20models.htm - Volume in Feet

If you don't feel like Downloading the file You can copy it:

Option Explicit
Dim swApp As SldWorks.SldWorks
Dim Part As SldWorks.ModelDoc2

Sub main()

Dim ThisFile As String
Dim Methods(8) As String
Dim Names As Variant
Dim Types As Variant
Dim Values As Variant
Dim editBody As body2
Dim swmacrofeaturebydefault As Variant

Set swApp = Application.SldWorks
Set Part = swApp.ActiveDoc
Set editBody = Nothing

'Define the routines to call
ThisFile = swApp.GetCurrentMacroPathName
Methods(0) = ThisFile
Methods(1) = "MacroFeature_Module1"
Methods(2) = "swmRebuild"
Methods(3) = ThisFile
Methods(4) = "MacroFeature_Module1"
Methods(5) = "swmEditDefinition"
Methods(6) = "" 'A security routine is optional
Methods(7) = ""
Methods(8) = ""

Names = Empty
Types = Empty
Values = Empty

'Insert the macro feature
Part.FeatureManager.InsertMacroFeature "Volume in Feet", "", (Methods), Names, Types, Values, editBody, swmacrofeaturebydefault

End Sub

Function swmRebuild(app As Variant, Part As Variant, feature As Variant) As Variant

Dim Definition As SldWorks.MacroFeatureData
Dim Objects As Variant
Dim ObjTypes As Variant
Dim SelMarks As Variant
Dim DrwViews As Variant

Set swApp = Application.SldWorks
Set Part = swApp.ActiveDoc
Set Definition = feature.GetDefinition

Definition.GetSelections2 Objects, ObjTypes, SelMarks, DrwViews

Dim vol As Variant
Dim conv As Variant
Dim convRounded As String
Dim prop As Boolean
Dim mass As SldWorks.MassProperty

Set mass = Part.Extension.CreateMassProperty

vol = mass.volume

conv = vol * 35.31466672

convRounded = Round(conv, 3)

prop = Part.DeleteCustomInfo2("", "Volume")

prop = Part.AddCustomInfo3("", "Volume", 30, convRounded + " Cubic Feet")
Debug.Print " Volume = " & mass.volume

End Function

Function swmEditDefinition(app As Variant, Part As Variant, feature As Variant) As Variant
End Function
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search