Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.
benjamin.herinckx
451 Views, 2 Replies

Controlling Offset Orientation

Hi all,

v: Inventor Professional 2019

 

I'm trying to automate the hole command in the given fashion :

1 - select face

2 - project face

3 - offset projected lines

4 - Apply hole command to the hole centers

 

The code I have come up with works (more or less). 

However on Step 3, the orientation of the projected line seem to alternate between inwards and outwards (should always be inwards)..

 

You will find a commented out section in my code trying to deal with the matter but it is not working as expected (that is, it does nothing..)

 

Can someone help me define the proper way to always offset a geometry inwards ?

 

Dim oDoc As Document = ThisApplication.ActiveDocument
Dim oCD As ComponentDefinition = oDoc.ComponentDefinition
Dim TG As TransientGeometry = ThisApplication.TransientGeometry
Dim oFace As Face
Dim oOffsetDir As Boolean = False

Do
oFace = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kPartFacePlanarFilter, "Select Face to Place Sketch")
    If Not oFace Is Nothing Then 
        Dim oSketch As Sketch = oCD.Sketches.Add(oFace, True)
        Dim oLineCollection As ObjectCollection = ThisApplication.TransientObjects.CreateObjectCollection
        Dim oPointCollection As ObjectCollection = ThisApplication.TransientObjects.CreateObjectCollection
        For Each oLine As SketchLine In oSketch.SketchLines
            oLine.Construction = True
            oLineCollection.Add(oLine)
        Next

        'Define the offset direction
        ' Dim Params(0 To 1) As Double
        ' Dim Normals(0 To 2) As Double
        ' Params(0) = 0 
        ' Params(1) = 0
        ' oFace.Evaluator.GetNormal(Params, Normals)
        ' Dim oNormalVector As UnitVector = TG.CreateUnitVector(Normals(0), Normals(1), Normals(2))
        ' Dim oLineVector As UnitVector = TG.CreateUnitVector(oLineCollection.Item(1).Geometry.Direction.X,oLineCollection.Item(1).Geometry.Direction.Y,0)
        ' Dim oOffsetVector = oLineVector.CrossProduct(oNormalVector)

        ' If oOffsetVector.IsEqualTo(oDesiredVector) = True Then
        '     oOffsetDir = True
        ' Else 
        '     oOffsetDir = False  
        ' End If

        oSketch.OffsetSketchEntitiesUsingDistance(oLineCollection,40,oOffsetDir,False,True)

        For Each oLine As SketchLine In oSketch.SketchLines
            If oLine.Construction = False Then
                oPointCollection.Add(oLine.EndSketchPoint)
                oPointCollection.Add(oLine.StartSketchPoint)
            End If
        Next

        For Each oPoint As SketchPoint In oPointCollection
            oPoint.HoleCenter = True
        Next

        oCD.Features.HoleFeatures.AddDrilledByThroughAllExtent(oPointCollection1, kPositiveExtentDirection)
    End If
Loop While Not oFace Is Nothing

 

 

Hi Benjamin,

 

This is quick/dirty hack. Create both side lines and determine which line set is inside by the total length of them.

If the code will be used by just you, this may be enough.

 

 

option explicit on
Dim oDoc As Document = ThisApplication.ActiveDocument
Dim oCD As ComponentDefinition = oDoc.ComponentDefinition
Dim TG As TransientGeometry = ThisApplication.TransientGeometry
Dim oFace As Face

Do
oFace = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kPartFacePlanarFilter, "Select Face to Place Sketch")
    If Not oFace Is Nothing Then 
        Dim oSketch As Sketch = oCD.Sketches.Add(oFace, True)
        Dim oLineCollection As ObjectCollection = ThisApplication.TransientObjects.CreateObjectCollection
        Dim oPointCollection As ObjectCollection = ThisApplication.TransientObjects.CreateObjectCollection
        For Each oLine As SketchLine In oSketch.SketchLines
            oLine.Construction = True
            oLineCollection.Add(oLine)
        Next

        Dim createdSetOne As ObjectCollection = ThisApplication.TransientObjects.CreateObjectCollection
        Dim createdSetTwo As ObjectCollection = ThisApplication.TransientObjects.CreateObjectCollection
        Dim createdTotalLengthOne As Double
        Dim createdTotalLengthTwo As Double
        
        For Each oLine As SketchLine In oSketch.OffsetSketchEntitiesUsingDistance(oLineCollection,40,True,False,True)
            createdTotalLengthOne = createdTotalLengthOne + oLine.Length
            createdSetOne.Add(oLine)
        Next oLine
        
        For Each oLine As SketchLine In oSketch.OffsetSketchEntitiesUsingDistance(oLineCollection,40,False,False,True)
            createdTotalLengthTwo = createdTotalLengthTwo + oLine.Length
            createdSetTwo.Add(oLine)
        Next oLine

        Dim insideSet As ObjectCollection
        Dim outsideSet As ObjectCollection
        
        If createdTotalLengthOne < createdTotalLengthTwo Then
            insideSet = createdSetOne
            outsideSet = createdSetTwo
        Else
            insideSet = createdSetTwo
            outsideSet = createdSetOne
        End If
            
        For Each oLine As SketchLine In insideSet
            oPointCollection.Add(oLine.EndSketchPoint)
            oPointCollection.Add(oLine.StartSketchPoint)
        Next

        For Each oLine As SketchLine In outsideSet
            oLine.Delete()
        Next

        For Each oPoint As SketchPoint In oPointCollection
            oPoint.HoleCenter = True
        Next

        oCD.Features.HoleFeatures.AddDrilledByThroughAllExtent(oPointCollection, 1, kPositiveExtentDirection)
    End If
Loop While Not oFace Is Nothing

 

=====

Freeradical

 Hideo Yamada

 

 

=====
Freeradical
 Hideo Yamada
https://www.freeradical.jp

Hi thnx for the input.

 

This is indeed a clever hack. Drawing both and keeping the shortest one.

However it doesn't work... which is weird.

I get the same behaviour with both pieces of code.

 

Does it work right on your machine ? Maybe I've got some obscure configuration wrong..

 

EDIT :

 

Fixed it, because of the DO WHILE you have to reset the length variables (otherwise it keeps adding itself)

        Dim createdTotalLengthOne As Double = 0
        Dim createdTotalLengthTwo As Double = 0

 Thank you very much !