Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

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

How to add QTY property to each part in the assembly 2

Status
Not open for further replies.

Fixturedesign

Mechanical
Nov 20, 2002
40
I am working for a company who still likes the old way of having the QTY of a part show up on the part drawing. They are also stuck with 2007 - they won't upgrade until their main customer does. So I am in a pickle.

I've detailed all the parts in separate drawings. There is no way I can find to link back to the assembly to grab the QTY. Can this be done?

I found some code from "handleman" here:
There are even sample files to help out - but I'm stuck in 2007. I can try the 2009 files tonight at home, but walking through the code here at work, it does not seem to work in 07. Any chance the code can be modified to work for my SolidWorks 2007?
 
Replies continue below

Recommended for you

OK- I went to the SolidWorks forum to find where handleman got his info. Looks like they are saying it will not work prior to 2009. It really blows my mind that someone wants to duplicate this information and chance getting the wrong QTY on a drawing #, when the master BOM tells it all. To top it off, we may build multiple sub assemblies at one time so the shop has to go through all the drawings and mark up the quantities any how. What ever! I get paid by the hour so I'll give them what they want.

I would still be interested to see if anyone has made this work in 2007 - probably using some other technique.
 
I have 100 unique parts in the main assembly. To create all these configurations would be confusing.
 
Oh, now you're just getting picky ... you want it easy and not-confusing. [smile]

Here's an older method (not a macro) by handleman
thread559-150852
 
Hi, Fixturedesign:

If I were you, I would take charge and change the practice used in your company.

A part has no quantity. It is only BOM that has quantity of the part. It is a bad idea not to follow ANSI standard.

Best regards,

Alex
 
While the linked method will work, it's going to be completely impractical for anything but very small assemblies. The procedure I had posted before still isn't a link. It's just an automated way of pushing the assembly quantity out to the part custom property whenever you rebuild. Not much better than just running a standalone macro.

Which brings us to said macro.

Below is a standalone macro VBA code from the automatic method. To use it:

In your main assembly, create a custom property called "Cfg4Qty". Its value should be the name of the config in which you wish to count quantities. The macro will only update parts quantities when this configuration is active. When you run the macro, it will iterate through all the parts in the model and add two custom properties. One will contain the quantity, and the other contains the name of the assembly and its configuration in which the quantities were counted.

Code:
Sub main()
Dim myAsy As AssemblyDoc
Dim myCmps
Dim Cfg As String
Dim CmpDoc As ModelDoc2
Dim i As Long
Dim j As Long
Dim cCnt As Long
Dim NoUp As Long
Dim myCmp As Component2
Dim tCmp As Component2
Dim tm As Double
Dim swApp As SldWorks.SldWorks
Dim swDoc As SldWorks.ModelDoc2

Set swApp = Application.SldWorks
Set swDoc = swApp.ActiveDoc
tm = Timer
Set myAsy = swDoc

If myAsy.ConfigurationManager.ActiveConfiguration.Name <> myAsy.CustomInfo2("", "Cfg4Qty") Then
 MsgBox "Qtys not updated due to config"
 Exit Sub
End If
NoUp = 0
myCmps = myAsy.GetComponents(False)
For i = 0 To UBound(myCmps)
 Set myCmp = myCmps(i)
 If (myCmp.GetSuppression = 3) Or (myCmp.GetSuppression = 2) Then
  cCnt = 0
  Set CmpDoc = myCmp.GetModelDoc
  Cfg = myCmp.ReferencedConfiguration
  For j = 0 To UBound(myCmps)
   Set tCmp = myCmps(j)
   If tCmp.GetSuppression <> 0 Then
    If tCmp.GetModelDoc2 Is CmpDoc Then
     If tCmp.ReferencedConfiguration = Cfg Then
      cCnt = cCnt + 1
     End If
    End If
   End If
  Next j
  CmpDoc.AddCustomInfo3 Cfg, "AutoQty", 30, ""
  CmpDoc.AddCustomInfo3 Cfg, "QtyIn", 30, ""
  CmpDoc.CustomInfo2(Cfg, "AutoQty") = cCnt
  CmpDoc.CustomInfo2(Cfg, "QtyIn") = myAsy.GetTitle & " Cfg " & myAsy.ConfigurationManager.ActiveConfiguration.Name
 Else
  NoUp = NoUp + 1
 End If
Next i
MsgBox NoUp & " Parts not updated due to lightweight (" & Timer - tm & "s)"
End Sub

-handleman, CSWP (The new, easy test)
 
Handleman - you are very gracious to offer your services. Thanks. I tried the new macro and it stops each time, with the debugger highlighting the line

Code:
    If tCmp.GetModelDoc2 Is CmpDoc Then

Is this 2007 compatible?
 
rgrayclamps - I agree with your statement, and am trying to make this happen. I've been here three months and see much need for improvement, but as the new guy I have to understand that they are not going to accept everything I feel needs changed. Something has worked for them for years - even though it slows down their productivity. So I am looking for better solutions to help automate the redundant tasks they feel are necessary.
 
Ah. GetModelDoc2 is new for 2009. Just change the line to

If tCmp.GetModelDoc Is CmpDoc Then

Shouldn't change the function at all for this macro.

-handleman, CSWP (The new, easy test)
 
handleman - that did the trick. I've test it and it works as intended for this exercise. I'm surprised at how short the code is and how quickly you were able to put this together. Thanks.
 
"put this together" is certainly the correct terminology here. If you'll examine the code, you'll see that the finished code is almost identical (just a few minor changes) to the two halves of code in the thread you linked. :) Doesn't take long at all.

It's pretty brute-force, but SW is fast like that.

-handleman, CSWP (The new, easy test)
 
The other way you can do it is to read all children, check for duplicates while building a master part/assy array,do the math (read the main assy QTY first), and stuff the results in a custom property (like QTY). In 2008, you could do this with recursives, but they don't work in 2009 (unless its called a very few times) so the solution is much less elegant.

We don't have configurations that have to be counted except for stuff the macro ignors (on purpose) like fasteners and welds.

It will not work with suppressed or lightweight components. It will work with large assys.

Sorry, my employer would probably get emotional if I posted the macro.

John
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor