Autonumbering Based on Operation

Hello all,

I am kind of stumped. I have started to make a macro that will number our op sheets based on operation number. I can currently count each op sheet which and the macro will start back at one when the operation number changes, but I cant seem to figure out how to get it to count the total number of sheets per operation then do the individual number.

<p><span style="font-family: Consolas;">Sub main()<br>    Dim swApp           As SldWorks.SldWorks<br>    Dim swModel         As SldWorks.ModelDoc2<br>    Dim swDrawing       As SldWorks.DrawingDoc<br>    Dim swSelMgr        As SldWorks.SelectionMgr<br>    Dim swNote          As SldWorks.Note<br>    Dim swView          As SldWorks.View<br>    Dim swSheet         As SldWorks.Sheet<br>    Dim swAnno          As SldWorks.Annotation<br>    <br>    <br>    <br>    Dim swSelObj            As Object<br>    <br>    <br>    Dim SheetCount, e, k, t As Integer<br>    Dim i, j, x, y          As Integer</span></p><p><span style="font-family: Consolas;">    <br>    Dim strop               As String<br>    Dim strop2              As String<br>    Dim descriptionText     As String<br>    Dim SheetNames()        As String<br>    <br>    <br>    Dim sheets(0)           As Long<br>        <br>    <br>    Dim foundNote           As Boolean<br>    Dim ret                 As Boolean</span></p><p><span style="font-family: Consolas;"><br>    Set swApp = Application.SldWorks<br>    Set swModel = swApp.ActiveDoc<br>    Set swDrawing = swModel<br>    SheetNames = swDrawing.GetSheetNames<br>    swDrawing.ActivateSheet SheetNames(0)<br>    SheetCount = swDrawing.GetSheetCount<br>    <br>    If swApp.ActiveDoc.GetType <> swDocDRAWING Then<br>        swApp.SendMsgToUser2 "This Macro Is Only For Use On Drawing Documents!", swMbWarning, swMbOk<br>        End<br>    End If</span></p><p></p><p>t = 1<br>For e = 1 To SheetCount<br>strop2 = strop<br>    If e > 1 Then swDrawing.SheetNext<br>        swDrawing.EditTemplate<br>        Set swView = swDrawing.GetFirstView<br>        Set swNote = swView.GetFirstNote<br>        Do While Not swNote Is Nothing<br>            If (swNote.GetName = "DetailItem32") Then<br>                foundNote = True<br>                strop = swNote.GetText<br>            End If<br>            Set swNote = swNote.GetNext<br>        Loop<br>        <br>        t = t + 1<br>    <br>    If strop2 <> strop Then t = 1<br>    <br>    swApp.SendMsgToUser ("OP #" & strop & " : " & t)<br>    Next i<br>    <br>   <br>    <br>    swDrawing.EditSheet</p><p>Next e</p><p></p><p>swDrawing.ForceRebuild3 True</p><p>End Sub</p>
SolidworksApi macros
Answers 18
DG
DG 2017-07-17 Best Answer

, try using the ReDim Preserve method as demonstrated by here

MB
MB 2017-07-17

Thank you  , I will try to give that a shot. I have been playing around with the code for a little while and currently the only part that I can't figure out is how to count the total number of sheets used when the note ="OP(whatever the op is)". I attached the current iteration of the macro.

As it stands now. It will delete any existing "SHEET _ OF _" and replaces it with a running total based on Note #32, which is the Operation number.

I was looking at the definition of ReDim and I am not sure how this would allow me to accomplish what I am looking for.

DG
DG 2017-07-17

Can you attach some files to look at and adjust this macro?

MB
MB 2017-07-17

Sadly I cannot as it is against my companies policy. I will say that detailitem32 is referring to $PRPSHEET:{OPERATION NO.}, which is a config specific note on our template.

JA
JA 2017-07-17

You need to loop over the sheets in your document twice, once to count the totals and a second time to perform the changes that you are making in your existing macro.

Because your method of enumerating sheets is unique, I don't think there is a cleaner way of getting the totals than to count them.

You already have the iterator "t" being set to 1 every time a new Op is detected. Just append that value to an array "opSheetCounts" or something of that ilk before setting it to 1.

MB
MB 2017-07-18

I think you are right. Sadly Arrays are not my strong suit, so I might try to loop until conditions are met then goto and kind of jump around.

IK
IK 2017-07-18

is this what you want?

MB
MB 2017-07-18

Thanks Ivana, but that isn't quite what I need. I will try to explain it a little better. Each drawing document contains all of our operation sheets for 1 part number. So lets say part number 1 has 5 operations. If operation 2 has 5 sheets total, I need the macro to output sheet 1 of 5 for the first sheet then continue until the next operation.

JA
JA 2017-07-18

GoTo gets confusing quickly, hard to predict, hard to debug.

The syntax for arrays in VBA has a lot of overhead but they are very handy. I normally use the following pattern:

<p>Dim my_array() as Object 'replace Object with any type you want</p><p></p><p>redim my_array(0)</p><p>my_array(0) = Nothing 'set the first element to nothing explicitly. I don't think it is necessary but it gives me peace of mind.</p><p></p><p>'this block goes in your loop.</p><p>if test_if_array_needs_new_element then 'in your case, you want to append a new element every time the Op changes.</p><p>     if my_array(0) is Nothing then 'this is how I check if the first element has been assigned yet</p><p>          set my_array(0) = thing_you_want_to_append</p><p>     else 'if the first element has already been assigned</p><p>          redim preserve my_array(ubound(my_array)+1) '<span style="background-color: #f6f6f6;">resize the array </span></p><p>          set my_array(ubound(my_array)) = thing_you_want_to_append '<span style="background-color: #f6f6f6;">append to the newly created index</span></p><p>     end if</p><p>end if</p>

This isn't efficient for large arrays but it is quick to implement. It isn't efficient because redim preserve creates a copy of the array and copies the old array into the new one. For your purposes, Solidworks would probably crash from all the sheets before it saw a substantial performance difference from using redim preserve on every append.

IK
IK 2017-07-18

and this?

MB
MB 2017-07-18

Ivana that worked great. Thank you.

MB
MB 2017-07-20

I have one more small issue with this. Is there a way I can make the macro only search the sheet format for the note I want to delete? We usually have a revision history sheet which always contains the work "SHEET" and when I run the macro as it stands it will not only delete the sheet count, "SHEET 1 OF 1", but it will delete the history sheet note as well.

Thank you,

Matt

IK
IK 2017-07-20

I would set name to created note and use this name for removing.

something like this

MB
MB 2017-07-20

Thanks Ivana, I currently have that in the code, but if I am too explicit then it wont delete all of the current sheets numbers and if I am not explicit enough then it will delete the note. The other issue is this will be used by multiple people, and while I have no problem avoiding the use of certain phrases I cant control what others do.

IK
IK 2017-07-20

therefore gives this macro unique name to new created note. But it will not reach already created notes in past.

MB
MB 2017-07-20

The only issue with this is we cannot adjust our current template without customer approval, and then that would also force everyone to use the macro, which I am not so sure everyone is accepting of. Is it possible to search for the note in a given area?

IK
IK 2017-07-20
<p> If Trim(UCase(swNote.GetText)) Like "SHEET *# OF *#" Then</p><p>' delete note</p><p> End If</p>
MB
MB 2017-07-20

You are a life saver. Thank you so much, that works great.