Inventor Customization

Reply
Valued Contributor
nickv02
Posts: 68
Registered: ‎03-23-2012
Message 1 of 9 (734 Views)
Accepted Solution

VB.net - Hiding a Sketch or Changing Lineweight?

734 Views, 8 Replies
12-26-2012 09:42 AM

(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 ?

Valued Contributor
nickv02
Posts: 68
Registered: ‎03-23-2012
Message 2 of 9 (721 Views)

Re: VB.net - Hiding a Sketch or Changing Lineweight?

12-27-2012 10:58 AM 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

 

Valued Contributor
nickv02
Posts: 68
Registered: ‎03-23-2012
Message 3 of 9 (711 Views)

Re: VB.net - Hiding a Sketch or Changing Lineweight?

12-28-2012 12:59 PM 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.

Valued Contributor
nickv02
Posts: 68
Registered: ‎03-23-2012
Message 4 of 9 (693 Views)

Re: VB.net - Hiding a Sketch or Changing Lineweight?

01-07-2013 10:57 AM 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.

Employee
yuhanzhang
Posts: 423
Registered: ‎01-06-2009
Message 5 of 9 (687 Views)

Re: VB.net - Hiding a Sketch or Changing Lineweight?

01-07-2013 07:17 PM 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!



Rocky Zhang
Inventor API
Manufacturing Solutions
Autodesk, Inc.

Valued Contributor
nickv02
Posts: 68
Registered: ‎03-23-2012
Message 6 of 9 (677 Views)

Re: VB.net - Hiding a Sketch or Changing Lineweight?

01-08-2013 06:50 AM 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

 

Valued Contributor
nickv02
Posts: 68
Registered: ‎03-23-2012
Message 7 of 9 (664 Views)

Re: VB.net - Hiding a Sketch or Changing Lineweight?

01-09-2013 11:00 AM 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.

Employee
yuhanzhang
Posts: 423
Registered: ‎01-06-2009
Message 8 of 9 (661 Views)

Re: VB.net - Hiding a Sketch or Changing Lineweight?

01-09-2013 07:33 PM 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

 



Rocky Zhang
Inventor API
Manufacturing Solutions
Autodesk, Inc.

Valued Contributor
nickv02
Posts: 68
Registered: ‎03-23-2012
Message 9 of 9 (653 Views)

Re: VB.net - Hiding a Sketch or Changing Lineweight?

01-10-2013 06:40 AM 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!

 

You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.

Need installation help?

Start with some of our most frequented solutions to get help installing your software.

Ask the Community


Inventor Exchange Apps

Created by the community for the community, Autodesk Exchange Apps for Autodesk Inventor helps you achieve greater speed, accuracy, and automation from concept to manufacturing.

Connect with Inventor

Twitter

Facebook

Blogs

Pinterest

Youtube