Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

VB.net - Hiding a Sketch or Changing Lineweight?

8 REPLIES 8
SOLVED
Reply
Message 1 of 9
nickv02
1943 Views, 8 Replies

VB.net - Hiding a Sketch or Changing Lineweight?

(Image attached). I am using Inventor 2013 and Visual Basic 2010 Express.  I created a sketch within one of my views on my .idw.  I would like to turn this sketch on or off based on the input from a variable that is read elsewhere.  I have used the following code from the help file with no luck so far.

 

Public Sub SketchEdit()

        Dim oDrawDoc As DrawingDocument
        oDrawDoc = _invApp.ActiveDocument
        ' Determine if there are any sheet overlay sketches.
        Dim oSketches As DrawingSketches
        oSketches = oDrawDoc.ActiveSheet.Sketches
        If oSketches.Count = 0 Then
            Exit Sub
        End If
        ' Set a reference to the first sketch.
        Dim oSketch As DrawingSketch
        oSketch = oSketches.Item(4)

        oSketch.Edit()
        If vValue_Closer = "" Then
            oSketch.LineWeight = 0
        Else
            oSketch.LineWeight = 0.007
        End If
        ' Return from edit mode.
        oSketch.ExitEdit()
    End Sub

 The code errors out on the line 'osketch = osketches.item(4)'.  I have tried using a number like that, and I have also tried using the actual name of the sketch.  Neither works.

oSketch = oSketches.Item("Closer_Reinf")

 Any advice on what to edit here to get it to look at my Closer_Reinf sketch to hide it/unhide it ?

8 REPLIES 8
Message 2 of 9
nickv02
in reply to: nickv02

As it turns out, my error lied in how I referenced the sketch.  Making a reference to just a sketch only looks for sketches on the sheet, not within a view.  I had to declare my view, then the sketch within the view.  I also found that changing the color to white from black hid it from printing in the PDF, which proved to be easier than fooling with lineweights.

 

Here is the new fixed code for future reference in case anyone can utilize it.

 

Public Sub SketchEdit()

        Dim oDrawDoc As DrawingDocument
        oDrawDoc = _invApp.ActiveDocument

        Dim oSheet1_View As DrawingView = oDrawDoc.Sheets(1).DrawingViews(1)
        Dim oSketch As DrawingSketch
        oSketch = oSheet1_View.Sketches.Item(2)

        Dim oColor_White As Color
        Dim oColor_Black As Color
        oColor_White = _invApp.TransientObjects.CreateColor(255, 255, 255) 'White
        oColor_Black = _invApp.TransientObjects.CreateColor(0, 0, 0) 'Black

        oSketch.Edit()
        If vValue_Closer = "" Then
            oSketch.Color = oColor_White

        Else
            oSketch.LineType = LineTypeEnum.kDashedLineType
            oSketch.Color = oColor_Black

        End If
        ' Return from edit mode.
        oSketch.ExitEdit()

    End Sub

 

Message 3 of 9
nickv02
in reply to: nickv02

I thought I had it all figured out, and I was wrong.  Changing the line color to white works in some instances, but I have another instance where I need to figure out how to hide the entire sketch from a drawing view.  See attached pic.  I am trying to hide a sketch I have made within a drawing view.  The sketch is the hole representation.

 

My code is as follows:

 

Public Sub SketchEdit_Flange_Holes()

        Dim oDrawDoc As DrawingDocument
        oDrawDoc = _invApp.ActiveDocument
        Dim oColor_White As Color
        Dim oColor_Black As Color
        oColor_White = _invApp.TransientObjects.CreateColor(255, 255, 255) 'White
        oColor_Black = _invApp.TransientObjects.CreateColor(0, 0, 0) 'Black

        Dim oSheet1_View As DrawingView = oDrawDoc.Sheets(1).DrawingViews(4) 'View 4 - Section Cut Side View
        Dim oSketch_Top_Holes As DrawingSketch
        Dim oSketch_Bott_Holes As DrawingSketch
        oSketch_Top_Holes = oSheet1_View.Sketches.Item(2)

        oSketch_Top_Holes.Edit()
        If vValue_Flange_Holes = "N" Then

            oSketch_Top_Holes.Visible = False

        ElseIf vValue_Flange_Holes = "Y" Then

            oSketch_Top_Holes.Visible = True

        End If
        ' Return from edit mode.
        oSketch_Top_Holes.ExitEdit()

        If vValue_SillType = "F6" Then

            oSketch_Bott_Holes = oSheet1_View.Sketches.Item(3)
            oSketch_Bott_Holes.Edit()
            If vValue_Flange_Holes = "N" Then

                oSketch_Bott_Holes.Visible = False

            ElseIf vValue_Flange_Holes = "Y" Then
                
                oSketch_Bott_Holes.Visible = True

            End If
            ' Return from edit mode.
            oSketch_Bott_Holes.ExitEdit()
        Else 'Do Nothing

        End If

    End Sub

I tried turning it white like I did on the previous example, but because of it's placement, it whites out the lines behind it and that leaves gaps in my drawing.  Plus it won't white out the filled portion.

 

When I run the code, it will throw an error on the first oSketch_Top_Holes.Visible = False line.  The weird thing is, it won't throw an error on the oSketch_Top_Holes.Visible = True line.  My assumption from my research is that the oSketch.Visible = True/False code is meant for part documents, not drawing documents.  If that is the case, can someone point me towards the code required to suppress sketches in drawing views?

 

I have also tried omitting going in to Sketch Edit mode using the oSketch.Edit command, but that didn't work either.

Message 4 of 9
nickv02
in reply to: nickv02

Still looking for VB help to hide a sketch within a drawing view.  If anyone has guidance, please post it up.

Message 5 of 9
YuhanZhang
in reply to: nickv02

Set DrawingSketch.Visible = False is not working, in UI you can see the drawing sketch has not visibility command in its context menu. 

 

I think what you want to do is to hide the drawing objects so they won't be printed. For drawing sketch curves you can set their SketchOnly = True to hide them, but for the hatched/filled regions currently there is no APIs to hide them(for filled regions you can either delete them or set the same color as the background, while for the hatched regions currently there is no API to access them).

 

So if your drawing sketch has only curves or filled regions that you can change them as I suggested above to hide them from printing.

 

Hope this helps!



If this solves the problem please click ACCEPT SOLUTION so other people can find it easily.



Rocky Zhang
Inventor API PD
Manufacturing Solutions
Autodesk, Inc.

Message 6 of 9
nickv02
in reply to: YuhanZhang

Thank you for the response!  Using your 'SketchOnly' term, I was able to find some more info on the matter, and came up with some code that works for turning on and off my sketched rectangle in one view.  I haven't attempted to hide the filled region I have yet, that is coming next.  Just wanted to share the code and say thanks again.

 

Public Sub SketchEdit_Closer_Reinf()

        Dim oDrawDoc As DrawingDocument
        oDrawDoc = _invApp.ActiveDocument
        Dim oSketches As DrawingSketches
        oSketches = oDrawDoc.ActiveSheet.Sketches

        Dim oSheet1_View As DrawingView = oDrawDoc.Sheets(1).DrawingViews(1)
        Dim oSketch As DrawingSketch
        oSketch = oSheet1_View.Sketches.Item(2)

        oSketch.Edit()

        If vValue_Closer = "" Then

            Dim oLines As SketchLines
            oLines = oSketch.SketchLines

            Dim oSelectset As SelectSet
            oSelectset = _invApp.ActiveDocument.SelectSet
            oSelectset.Clear()

            Dim oLine As SketchLine
          
            For Each oLine In oLines
                oSelectset.Select(oLine)
                oLine.SketchOnly = True

            Next

        Else

            Dim oLines As SketchLines
            oLines = oSketch.SketchLines

            Dim oSelectset As SelectSet
            oSelectset = _invApp.ActiveDocument.SelectSet
            oSelectset.Clear()

            Dim oLine As SketchLine

            For Each oLine In oLines
                oSelectset.Select(oLine)
                oLine.SketchOnly = False

            Next

        End If

        oSketch.ExitEdit()

    End Sub

 

Message 7 of 9
nickv02
in reply to: YuhanZhang


yuhanzhang wrote:, but for the hatched/filled regions currently there is no APIs to hide them(for filled regions you can either delete them or set the same color as the background, while for the hatched regions currently there is no API to access them).

 



Can you shed some light on the code required to access the filled regions and change their color?  So far I've had no luck.

 

oSketch.SketchFilledRegions has no 'Color' attribute to set.  Only Count, Address, Item, and Type. 

 

and

Dim oSketchFill As SketchFillRegion
oSketchFill.Color = oColor_White

does not want to work either.

Message 8 of 9
YuhanZhang
in reply to: nickv02

Below is VBA code sample, you can have a try:

Sub Sample()
    Dim oDrawingDoc As DrawingDocument
    Set oDrawingDoc = ThisApplication.ActiveDocument
    
    Dim oSheet As Sheet
    For Each oSheet In oDrawingDoc.Sheets
         
        Dim oSketch As DrawingSketch
        
        For Each oSketch In oDrawingDoc.Sheets(1).Sketches
            Call ChangeFillRegionColorAndHideCurves(oSketch, oDrawingDoc)
        Next
        
        Dim oDrawingView As DrawingView
        For Each oDrawingView In oSheet.DrawingViews
            For Each oSketch In oDrawingView.Sketches
                Call ChangeFillRegionColorAndHideCurves(oSketch, oDrawingDoc)
            Next
        Next
    Next
End Sub


Sub ChangeFillRegionColorAndHideCurves(oSketch As DrawingSketch, oDrawingDoc As DrawingDocument)
    Dim oFillRegion As SketchFillRegion, oEntity As SketchEntity
    oSketch.Edit
    
    ' Change the fill region color to be same as sheet color
    For Each oFillRegion In oSketch.SketchFillRegions
        oFillRegion.Color = oDrawingDoc.SheetSettings.SheetColor
    Next
    
    ' Set the sketch curves to be visible in sketch mode only
    For Each oEntity In oSketch.SketchEntities
        If Not oEntity.Type = kSketchPointObject Then
            oEntity.SketchOnly = True
        End If
    Next

    oSketch.ExitEdit
End Sub

 



If this solves the problem please click ACCEPT SOLUTION so other people can find it easily.



Rocky Zhang
Inventor API PD
Manufacturing Solutions
Autodesk, Inc.

Message 9 of 9
nickv02
in reply to: YuhanZhang

That helps immensely, this has all helped me over a huge hurdle I've been having with my drawings.  Thank you so much!

 

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report