Search code examples
vbasolidworks

How to change Hole Table axis orientation via SolidWorks API?


Is there a way to change orientation (direction) of Hole Table axes with SolidWorks API?

I can do it manually by dragging the handles but recorded VBA macro does not contain actual changes.

This is what I would like to achieve:

Before
before

After
enter image description here

I don't have Visual Studio Tools on this PC so I cannot record a C# or VB macro and see if it contains more code. If somebody could check that on their PC I would be grateful.


Solution

  • I have figured it out. This time digging through SolidWorks API Help was useful.

    By using HoleTable.DatumOrigin.SetAxisPoints() method it is possible to change points that define the Hole Table axes.
    Important to notice is that SetAxisPoints() changes only the end points of the axis arrows (tips of the arrowheads). Start points get updated automatically.

    You can get current point values with HoleTable.DatumOrigin.GetAxisPoints2() method.

    Another thing to notice is that values in the hole table do not get updated automatically. They did update after I manually dragged a an axis point. To get them update by the code set HoleTable.EnableUpdate property to False before and back to True after the call to SetAxisPoints().

    Here is the code excerpt that does what I needed:

    Dim ht As SldWorks.HoleTable
    Dim htdo As SldWorks.DatumOrigin
    Dim htdaxpts() As Double
    Dim htdaxptsnew(0 To 3) As Double
    Dim ystarty As Double
    Dim yendx As Double
    Dim yendy As Double
    Dim xstartx As Double
    Dim xendx As Double
    Dim xendy As Double
    
        '...
        'here comes code to prepare for Hole Table insertion
        '...
    
        'insert the Hole Table
        Set htann = theView.InsertHoleTable2(False, anchorx, anchory, swBOMConfigurationAnchor_BottomLeft, "A", holetemplatepath)
    
        If Not htann Is Nothing Then
            Set ht = htann.HoleTable
            Set htdo = ht.DatumOrigin
    
            'disable hole table update to get it refresh when done
            ht.EnableUpdate = False
    
            'get coordinates of the axis arrows (4 pairs of (x,y) doubles: X start(0,1), X end(2,3), Y start(4,5), Y end(6,7))
            htdaxpts = htdo.GetAxisPoints2()
            'take the values we use
            xstartx = htdaxpts(0)
            xendx = htdaxpts(2)
            xendy = htdaxpts(3)
            ystarty = htdaxpts(5)
            yendx = htdaxpts(6)
            yendy = htdaxpts(7)
            'change direction only if Y arrow points up
            If ystarty < yendy Then
                yendy = ystarty - (yendy - ystarty)
            End If
            'change direction only if X arrow points left
            If xstartx > xendx Then
                xendx = xstartx - (xendx - xstartx)
            End If
            'change position only if X arrow is below Y arrow
            If xendy < ystarty Then
                'we can change end point only so change X end y only
                xendy = xendy + (ystarty - xendy) * 2
            End If
            'prepare new axis points (2 pairs of (x,y) doubles: X end(0,1), Y end(2,3))
            htdaxptsnew(0) = xendx
            htdaxptsnew(1) = xendy
            htdaxptsnew(2) = yendx
            htdaxptsnew(3) = yendy
            'set new axis end points
            htdo.SetAxisPoints htdaxptsnew
    
            'enable hole table update to refresh the values
            ht.EnableUpdate = True
    
        End If