Loop through all sheets

Loop through all sheets

Anonymous
Not applicable
3,383 Views
2 Replies
Message 1 of 3

Loop through all sheets

Anonymous
Not applicable

Hi,

 

I have adapted code I found for changing the layer of sketch geometry, within a sketch created in a drawing view. But I can't manage to adapt it to loop through all sheets. Below is the code that works for the active sheet only.

 

How do I get this code to loop through all sheets winthin the .idw?

 

Thanks for your help

M

 

' Set a reference to the drawing document.

' This assumes a drawing document is active.

Dim oDrawDoc As DrawingDocument = ThisApplication.ActiveDocument

' Set a reference to the active sheet.

Dim oActiveSheet As Sheet

oActiveSheet = oDrawDoc.ActiveSheet

 

' Create a Drawing View Object

Dim oDrawingView As DrawingView

Dim oDrawingSketch As DrawingSketch

Dim oSketchEntity As SketchEntity

 

Dim oLayer As Layer

oLayer = oDrawDoc.StylesManager.Layers.Item("test")

 

For Each oDrawingView In oActiveSheet.DrawingViews

   For Each oDrawingSketch In oDrawingView.Sketches

           If oDrawingSketch.Name = "Workpiece Size" Then

               oDrawingSketch.Edit

               For Each oSketchEntity In oDrawingSketch.SketchEntities

                   oSketchEntity.Layer = oLayer

               Next

               oDrawingSketch.ExitEdit

 

           End If

   Next

Next

 

iLogicVb.UpdateWhenDone = True

0 Likes
Accepted solutions (1)
3,384 Views
2 Replies
Replies (2)
Message 2 of 3

MechMachineMan
Advisor
Advisor
Accepted solution
Sub Main()
    oDrawDoc = ThisApplication.ActiveDocument
    oLayer = oDrawDoc.StylesManager.Layers.Item("test")

    For Each oSheet in oDrawDoc.Sheets
        ProcessSheet(oSheet)
    Next

    oDrawDoc.Update
End Sub

Dim oDrawDoc As DrawingDocument
Dim oLayer As Layer

Sub ProcessSheet(oSheet As Sheet)

    Dim oDrawingView As DrawingView
    Dim oDrawingSketch As DrawingSketch
    Dim oSketchEntity As SketchEntity
 
    For Each oDrawingView In oSheet.DrawingViews
       For Each oDrawingSketch In oDrawingView.Sketches
           If oDrawingSketch.Name = "Workpiece Size" Then
               oDrawingSketch.Edit

               For Each oSketchEntity In oDrawingSketch.SketchEntities
                   oSketchEntity.Layer = oLayer
               Next

               oDrawingSketch.ExitEdit
           End If
       Next
    Next
End Sub

--------------------------------------
Did you find this reply helpful ? If so please use the 'Accept as Solution' or 'Like' button below.

Justin K
Inventor 2018.2.3, Build 227 | Excel 2013+ VBA
ERP/CAD Communication | Custom Scripting
Machine Design | Process Optimization


iLogic/Inventor API: Autodesk Online Help | API Shortcut In Google Chrome | iLogic API Documentation
Vb.Net/VBA Programming: MSDN | Stackoverflow | Excel Object Model
Inventor API/VBA/Vb.Net Learning Resources: Forum Thread

Sample Solutions:Debugging in iLogic ( and Batch PDF Export Sample ) | API HasSaveCopyAs Issues |
BOM Export & Column Reorder | Reorient Skewed Part | Add Internal Profile Dogbones |
Run iLogic From VBA | Batch File Renaming| Continuous Pick/Rename Objects

Local Help: %PUBLIC%\Documents\Autodesk\Inventor 2018\Local Help

Ideas: Dockable/Customizable Property Browser | Section Line API/Thread Feature in Assembly/PartsList API Static Cells | Fourth BOM Type
0 Likes
Message 3 of 3

Anonymous
Not applicable

Hi,

 

Thanks, this works (sorry for the late reply). This code is perfect. I now need to work out how to replace the line "oLayer = oDrawDoc.StylesManager.Layers.Item("test")" with a layer name that is set per sheet from a promted entry that exists on each sheet. I have adapted code that I have found on cadlinecommunity. This loops through all sheets and creates a layer per sheet based on the promted entry.

 

Is it possible to replace "oLayer = oDrawDoc.StylesManager.Layers.Item("test")" with the new layer???

 

Thanks in advance for any help you can give me

M

 

 

 

On Error Resume Next

Dim oDrawDoc As DrawingDocument = ThisApplication.ActiveDocument

Dim oSheet As Sheet

Dim oPromptEntry

 

i = 1

For Each oSheet In oDrawDoc.Sheets

i = i+1

ThisApplication.ActiveDocument.Sheets.Item(i).Activate

       oTitleBlock=oSheet.TitleBlock

   oTextBoxes=oTitleBlock.Definition.Sketch.TextBoxes

   For Each oTextBox In oTitleBlock.Definition.Sketch.TextBoxes

   Select oTextBox.Text

       Case "{Item.Thickness}"

           oPromptEntry = oTitleBlock.GetResultText(oTextBox)

          

                       Dim oNEWlayer As Layer

                       oNEWlayer = oDrawDoc.StylesManager.Layers.Item("Workpiece Size").Copy("XLYP1H" & oPromptEntry)

 

   End Select

   Next

Next

 

iLogicVb.UpdateWhenDone = True

0 Likes