API Problem - Get thickness from Sheet metal part

Discussion in 'SolidWorks' started by SW Monkey, Nov 17, 2006.

  1. SW Monkey

    SW Monkey Guest

    I am trying to get the thickness from a sheet metal part. When I run
    the code, I get a Run-time error 424. Object required.

    Here is the code:

    START CODE
    -------------------------------
    Dim swApp As Object
    Dim Thickness As Double
    Dim model As ModelDoc2

    Sub main()

    Set swApp = Application.SldWorks
    Set model = swApp.ActiveDoc
    Thickness = SheetMetalFeatureData.Thickness


    End Sub
     
    SW Monkey, Nov 17, 2006
    #1
  2. SW Monkey

    JJ Guest

    Hello SW Monkey,

    I have a code that gets this value, although I cannot check it right
    now. I will look for the code next week.

    What you have to do is iterate the features and get the sheetmetal
    feature type (could be named differently). Then set the sheetmetal
    featuredata object and you can get the info.

    Kind regards,

    JJ
    www.studiozwaard.nl


    SW Monkey schreef:
     
    JJ, Nov 18, 2006
    #2
  3. SW Monkey

    JJ Guest

    Dim swsheetmetal As SldWorks.SheetMetalFeatureData

    Dim swapp As SldWorks.SldWorks

    Dim swModel As SldWorks.ModelDoc2


    sub main()
    Set swapp=createobject("Sldworks.application")
    Set swmodel=swapp.activedoc
    Set swfeat = swModel.FirstFeature
    While Not swfeat Is Nothing
    'Debug.Print swfeat.Name & " - " & swfeat.GetTypeName

    If swfeat.GetTypeName = "SheetMetal" Then
    Set swsheetmetal = swfeat.GetDefinition
    Thickness = swsheetmetal.Thickness

    End If

    Set swfeat = swfeat.GetNextFeature

    Wend
    end sub
     
    JJ, Nov 20, 2006
    #3
  4. SW Monkey

    SW Monkey Guest

    Thanks, that works.

    Im trying to get the thickness and also the volume so I can calculate
    the sq area in feet for the sheetmetal part. This is used in our MRP
    system for material qty. I tried to convert the thickness value to
    inches, but how can I display only 3 decimal places (it currently shows
    6)

    Next up, figuring out how to get the volume of the part.
     
    SW Monkey, Nov 20, 2006
    #4
  5. SW Monkey

    JJ Guest

    Hello SW Monkey,

    You can get the volume using API, or you can iterate all faces and get
    the largest face.

    The first one is the easiest.

    Else: this one is from the API help

    Option Explicit

    Sub main()

    Dim swApp As SldWorks.SldWorks

    Dim swModel As SldWorks.ModelDoc2

    Dim swModDocExt As SldWorks.ModelDocExtension

    Dim swMass As SldWorks.MassProperty

    Dim vCofG As Variant

    Dim vXaxis As Variant

    Dim vYAxis As Variant

    Dim vZAxis As Variant

    Dim swSkCofG As SldWorks.SketchPoint

    Dim swSkXaxis As SldWorks.SketchLine

    Dim swSkYaxis As SldWorks.SketchLine

    Dim swSkSegXaxis As SldWorks.SketchSegment

    Dim swSkSegYaxis As SldWorks.SketchSegment

    Dim swSelMgr As SldWorks.SelectionMgr

    Dim swSelData As SldWorks.SelectData

    Dim bRet As Boolean



    Set swApp = Application.SldWorks

    Set swModel = swApp.ActiveDoc

    Set swSelMgr = swModel.SelectionManager

    Set swSelData = swSelMgr.CreateSelectData

    Set swModDocExt = swModel.Extension

    Set swMass = swModDocExt.CreateMassProperty



    vCofG = swMass.CenterOfMass

    vXaxis = swMass.PrincipleAxesOfInertia(0)

    vYAxis = swMass.PrincipleAxesOfInertia(1)

    vZAxis = swMass.PrincipleAxesOfInertia(2)



    Debug.Print "File = " & swModel.GetPathName

    Debug.Print " Mass = " & swMass.Mass * 1000# & " g"

    Debug.Print " Surface Area = " & swMass.SurfaceArea * 1000000#
    & " mm^2"

    Debug.Print " Volume = " & swMass.Volume * 1000000000# &
    " mm^3"

    Debug.Print " Density = " & swMass.Density & " kg/m^3"

    Debug.Print " CenterOfMass = (" & vCofG(0) * 1000# & ", " &
    vCofG(1) * 1000# & ", " & vCofG(2) * 1000# & ") mm"

    Debug.Print " X axis = (" & vXaxis(0) & ", " & vXaxis(1)
    & ", " & vXaxis(2) & ")"

    Debug.Print " Y axis = (" & vYAxis(0) & ", " & vYAxis(1)
    & ", " & vYAxis(2) & ")"

    Debug.Print " Z axis = (" & vZAxis(0) & ", " & vZAxis(1)
    & ", " & vZAxis(2) & ")"



    swModel.Insert3DSketch2 False

    swModel.SetAddToDB True



    Set swSkCofG = swModel.CreatePoint2(vCofG(0), vCofG(1), vCofG(2))

    Set swSkXaxis = swModel.CreateLine2( _

    vCofG(0), vCofG(1), vCofG(2), _

    vCofG(0) + vXaxis(0), vCofG(1) + vXaxis(1),
    vCofG(2) + vXaxis(2))

    Set swSkYaxis = swModel.CreateLine2( _

    vCofG(0), vCofG(1), vCofG(2), _

    vCofG(0) + vYAxis(0), vCofG(1) + vYAxis(1),
    vCofG(2) + vYAxis(2))



    Set swSkSegXaxis = swSkXaxis

    Set swSkSegYaxis = swSkYaxis

    swModel.SetAddToDB False

    swModel.Insert3DSketch2 True



    swModel.ClearSelection2 True



    swSelData.mark = 1

    bRet = swSkCofG.Select4(True, swSelData): Debug.Assert bRet

    swSelData.mark = 2

    bRet = swSkSegXaxis.Select4(True, swSelData): Debug.Assert bRet

    swSelData.mark = 4

    bRet = swSkSegYaxis.Select4(True, swSelData): Debug.Assert bRet



    bRet = swModel.InsertCoordinateSystem(False, False, False):
    Debug.Assert bRet

    End Sub

    Kind regards,

    JJ
    www.studiozwaard.nl
     
    JJ, Nov 21, 2006
    #5
  6. SW Monkey

    SW Monkey Guest


    JJ,

    I would rather use

    retval = MassProperty.Volume (VB Get property)

    but I cant seem to get it to work correctly. I know that I can create
    a custom property called VOLUME, and the value is
    "". This will show the volume of the part in
    a custom property. How do I get this in my API?

    So far here is my code to get the thickness and show it in a message
    box.

    Dim swsheetmetal As SldWorks.SheetMetalFeatureData
    Dim swapp As SldWorks.SldWorks
    Dim swModel As SldWorks.ModelDoc2
    Dim conv As Double
    Sub main()
    Set swapp = CreateObject("Sldworks.application")
    Set swModel = swapp.ActiveDoc
    Set swfeat = swModel.FirstFeature
    While Not swfeat Is Nothing
    'Debug.Print swfeat.Name & " - " & swfeat.GetTypeName
    If swfeat.GetTypeName = "SheetMetal" Then
    Set swsheetmetal = swfeat.GetDefinition
    Thickness = swsheetmetal.Thickness
    End If
    Set swfeat = swfeat.GetNextFeature
    Wend
    conv = 39.37 'metric conversion factor
    Thickness = Thickness * conv
    Thickness = FormatNumber(Thickness, 3)
    MsgBox ("Thickness is" & Chr(13) & Thickness)
    End Sub
     
    SW Monkey, Nov 21, 2006
    #6
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.