Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

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

Part Density Using API 1

Status
Not open for further replies.

NirVidP

Industrial
May 10, 2010
45
Hello,

When I get the value of density using Density property of the MassProperty object it always returns the value of 1 irrespective of the material applied.

I have checked the help file and searched but can't find a clue?

What am I missing.

(I am new to SWk customization so not sure if the solution is obvious)
 
Replies continue below

Recommended for you

Hello,
Only the material is saved in the file.
The density, associated with this material, is stored in the material database.
Try the following code.
Note: go to Tools > Reference and add "Microsoft XML, VX.X"

Code:
Option Explicit
Sub main()
    Dim swApp As SldWorks.SldWorks
    Dim swModel As SldWorks.ModelDoc2
    Dim swPart As SldWorks.PartDoc
    Dim dbs As Variant
    Dim db As Variant
    Dim sMatName As String
    Dim sMatDB As String
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    Set swPart = swModel
    sMatName = swPart.GetMaterialPropertyName2("Default", sMatDB)
    Debug.Print "Material" & vbTab & sMatName
    dbs = swApp.GetMaterialDatabases
    For Each db In dbs
        If UCase(Left(Right(db, Len(sMatDB) + 7), Len(sMatDB))) = UCase(sMatDB) Then
            Call GetMatProperties(sMatName, CStr(db))
            Exit For
        End If
    Next
End Sub

Sub GetMatProperties(materialName As String, materialDatabaseFile As String)
    Dim xml_doc As New DOMDocument
    Dim onode   As IXMLDOMElement
    Dim pnode   As IXMLDOMNode
    Dim cnode   As IXMLDOMNode
    If Not xml_doc.Load(materialDatabaseFile) Then
        MsgBox "Unable to open XML File"
        Exit Sub
    End If
    For Each onode In xml_doc.selectNodes("//material")
        If onode.getAttribute("name") = materialName Then
            For Each cnode In onode.childNodes
                If cnode.baseName = "physicalproperties" Then
                    For Each pnode In cnode.childNodes
                        Debug.Print "  " & pnode.Attributes.Item(0).nodeValue & vbTab & pnode.Attributes.Item(1).nodeValue
                    Next
                End If
            Next
            Exit For
        End If
       Next
End Sub
 
fifi said:
Only the material is saved in the file.
False. Density is stored in the document.

Here's how to get it using the MassProperty object.
Sub main()
Dim swDoc As SldWorks.ModelDoc2
Dim swMprop As SldWorks.MassProperty

Set swDoc = Application.SldWorks.ActiveDoc
Set swMprop = swDoc.Extension.CreateMassProperty
Debug.Print swMprop.Density
End Sub

If you just want to get it from document properties you can use:
Sub main()
Dim swDoc As SldWorks.ModelDoc2

Set swDoc = Application.SldWorks.ActiveDoc
Debug.Print swDoc.GetUserPreferenceDoubleValue(swMaterialPropertyDensity)
End Sub

 
Thanks, its working.
Not sure what I did to mess up things earlier.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor