Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

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

Simpler way to get Mass Props from dwg view w/ VB 1

Status
Not open for further replies.

SWVB101

Mechanical
Jun 25, 2003
85
Is Their A Simpler Way To Programmatically Obtain the Mass Properties Of A Model, Given A View In A Drawing, WithOUT physically opening the Model In A new window...?

I know this is possible with Assemblies...
I don't see why it would be impossible with a drawing...

This is A Sample Senario of what I am trying to do...
Assuming the Current View in an Active Drawing is the view I want to retrieve the Mass Properties From...


Code:
  Dim swApp As Object, Model As Object, View As Object
  Dim data1 As Variant, data2 As Variant, data3 As Variant
  Dim mass As Double
  Const swDocPART = 1, swDocASSEMBLY = 2, swDocDRAWING = 3

  Set swApp = CreateObject("SldWorks.Application")
  Set Model = swApp.ActiveDoc
  Set View = Model.GetFirstView
  Set View = Model.GetNextView
  
  s$ = View.GetReferencedModelName()
  drawname$ = Model.GetPathName() 'Store current Doc
  Set Model = swApp.ActivateDoc(s$)
    data1 = Model.GetMassProperties()
    massp = round(data1(5) * 2.2046, 2)
    MP_Volume = round(data1(3) * 61023.74, 3)
    MP_Density = round(massp / MP_Volume, 3)

    '...Able to Change Density Here

    mass = Round(MP_Volume * MP_Density, 2)
  Set Model = swApp.ActivateDoc(drawname$) 'reStore Doc

And the Assembly Method (using component objects)...
Code:
  Set Configuration = Model.GetActiveConfiguration()
  Set Component = Configuration.GetRootComponent()

  If Component Is Nothing Then        ' If no component, then exit
      Exit Sub
  End If
        
  componentName = Component.Name      ' Get the component name
    
  Set modelD = Component.GetModelDoc()
  Data1 = Component.GetModelDoc().GetMassProperties()
  
  massp = Round((Data1(5) * 2.2046, 2)
  MP_Volume = Round((Data1(3) * 61023.74, 3)
  MP_Density = Round((massp / MP_Volume, 3)
    
  s1$ = "         Mass:" + Str(massp)
  s2$ = ",        Volume:" + Str(MP_Volume)
  s3$ = ",        Density:" + Str(MP_Density)

If you have any Ideas, or if you see something I missed, please let me know...
 
Replies continue below

Recommended for you

How about adding the mass properties of the model, as a configuration-specific property in the Model? This will get updated while the model is open, and you can then link to the model properties from your drawing view.
 
Thanks for your post and ideas
It's just not quite what I am looking for...

I would do this, myself, the trick is getting the other 300 SW users here to do the same... ;-)

The Top example works, but it has to physically open the document in another child window... and some times stays active... I could close it, but if it is already opened and not saved, SW does not warn you and closes it any way (bye, bye, changes)... I know this from experience (unfortunately)...

I know the model is loaded in memory when you open a drawing, due to the fact you can make views of it, shade the drawing, pull dimensions off of it, etc...

I am just looking for a "bridge" between the referenced document in a View Object and a Component Object and be able to derive a Model Object from this...

The reason for this is that we have a standard title block for our drawings...

I have made several programs to fill in the title block with an external App...

One of the title block fields is the weight of the part...

Like I said, so far the Top Example works 'OK' but could work better if it could get the Mass Props in the background, not by activateing a part model window...

Thanks,
--Josh--
 
Josh,

If the model is already loaded, which can be checked using

View.IsModelLoaded

then is is poosible to access the model using

Set Model = swApp.GetOpenDocumentByName(View.GetReferencedModelName())

This does not make the document visible, but does allow you to access the mass properties. :)

Also, I recommend using

Option Explicit

at the start of your module as it makes it easier to debug.

Nathan
 
Thanks,
I'll try that when I get back to work...

I had been looking for a GetModel() or GetPart() or something...

I forgot about swApp.GetOpenDocumentByName()

I figured I overlooked something...

btw... I do use Option Explicit...
That was a make-shift clip from a SolidWorks interface module I have been building...

All together it is about 500 - 700 lines, I didn't feel like pasting the whole thing...
 
Thanks,
...after you establish a View object...
This works great...

If Not View.IsModelLoaded() Then View.LoadModel
Set Model = swApp.GetOpenDocumentByName(View.GetReferencedModelName)

Data1 = Model.GetMassProperties()

massp = Round(Data1(5) * 2.2046, 4)
MP_Volume = Round(Data1(3) * 61023.74, 4)
MP_Density = Round(massp / MP_Volume, 3)

s1$ = " Mass:" + Str(massp)
s2$ = " Volume:" + Str(MP_Volume)
s3$ = " Density:" + Str(MP_Density)


Thanks for the Tip... Have a Star ;-)
 
BTW...

The BEST way to do this is like this...

Code:
'*** Model Mass Graber ** JDS 7/1/03 ***
  If Not View.IsModelLoaded() Then View.LoadModel
  Set Model = swApp.GetOpenDocumentByName(View.GetReferencedModelName)
Code:
Model.ShowConfiguration (View.ReferencedConfiguration)
Code:
  mpData = Model.GetMassProperties()
  
  massp = Round(mpData(5) * 2.2046, 4)
  MP_Volume = Round(mpData(3) * 61023.74, 4)
  MP_Density = Round(massp / MP_Volume, 3)
'***************************************
This Way you get the actual Mass Properties of the configuration in the view... Not whatever configuration it was last saved as...

Thanks,
--Josh--
 
No *wonder* my code seems to run so slow... Ive been coding in all BLACK INK .

 
My First star! I'm so happy :)

Glad I could help Josh.

Nathan
 
;-)... Yeah, the Blue ink is sure to work...

But don't use the green ink to often... It acts like it's not even there...[ghost]
And red ink gives you head aches and just doesn't like to run [hammer]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor