Macro - Hide Milling Circle (Drilling Assist)

Hello

I have a macro project to hide the milling circles on our MEPs (only on the 1st sheet and the 1st view)

Knowing that there are not always any.

1- Is it feasible in your opinion?

2-A little idea about the functions to start? (in particular how to identify if the circle is a circle of a milling)

 

Below is an example of the 2 types of countersinking encountered (normal or head clearance + added chambering):

Any lead that can help me get started, will be welcome.

I found this to put the circles in red but how to choose only the milling circles?

Sub main()
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set swDraw = swModel
Set swView = swDraw.ActiveDrawingView
swModel.ClearSelection2 True
vComps = swView.GetVisibleComponents
'Diameter = InputBox("Entrer le diamètre en mm")
For Each vComp In vComps
    Set Comp = vComp
    vEdges = swView.GetVisibleEntities(Comp, swViewEntityType_e.swViewEntityType_Edge)
    For Each vEdge In vEdges
        Set swEdge = vEdge
        Set swCurve = swEdge.GetCurve
        If swCurve.IsCircle Then
            swCurve.GetEndParams Empty, Empty, IsClosed, Empty
            If IsClosed Then
                CurveParam = swCurve.CircleParams
                'If Abs(Diameter - CurveParam(6) * 2 * 1000) < 0.0001 Then
                    Set swEntity = swEdge
                    swEntity.Select4 True, Nothing
                'End If
            End If
        End If
    Next
Next
swModel.SetLineColor 255
swModel.ClearSelection2 True
End Sub

Thank you.

1 Like

Try this:

Option Explicit
Sub main()
Dim swapp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swDraw As SldWorks.DrawingDoc
Dim swView As SldWorks.View
Dim vComps As Variant
Dim vComp As Variant
Dim Comp As SldWorks.Component2
Dim vEdges As Variant
Dim vEdge As Variant
Dim swEdge As SldWorks.edge
Dim swCurve As SldWorks.Curve
Dim IsClosed As Boolean
Dim pos As String
Dim CurveParam As Variant
Dim Cercle As Class1
Dim monCercle As Class1
Dim Cercles As Collection
Set swapp = Application.SldWorks
Set swModel = swapp.ActiveDoc
Set swDraw = swModel
Set swView = swDraw.ActiveDrawingView
If swView Is Nothing Then
    MsgBox "Selectionner une vue"
    Exit Sub
End If
Set Cercles = New Collection
vComps = swView.GetVisibleComponents
For Each vComp In vComps
    Set Comp = vComp
    vEdges = swView.GetVisibleEntities(Comp, swViewEntityType_e.swViewEntityType_Edge)
    For Each vEdge In vEdges
        Set swEdge = vEdge
        Set swCurve = swEdge.GetCurve
        If swCurve.IsCircle Then
            swCurve.GetEndParams Empty, Empty, IsClosed, Empty
            If IsClosed Then
                CurveParam = swCurve.CircleParams
                pos = ""
                If Round(CurveParam(3), 3) = 0 Then pos = pos & Round(CurveParam(0), 3) & "-"
                If Round(CurveParam(4), 3) = 0 Then pos = pos & Round(CurveParam(1), 3) & "-"
                If Round(CurveParam(5), 3) = 0 Then pos = pos & Round(CurveParam(2), 3) & "-"
                Debug.Print pos
                Set Cercle = New Class1
                Cercle.pos = pos
                Cercle.dia = Round(CurveParam(6), 3)
                Set Cercle.edge = swEdge
                Cercles.Add Cercle
            End If
        End If
    Next
Next
swModel.ClearSelection2 True
For Each monCercle In Cercles
    For Each Cercle In Cercles
        If Cercle.pos = monCercle.pos And Cercle.dia < monCercle.dia Then
          monCercle.edge.Select4 True, Nothing
        End If
    Next
Next
swDraw.HideEdge
swModel.ForceRebuild3 False
'swModel.SetLineColor 255'
swModel.ClearSelection2 True
End Sub

Insert > Class Module > name: Class1

'Class1:'
Public edge As SldWorks.Entity
Public pos As String
Public dia As Single

 

3 Likes

Once again perfectly functional. Thank you once again JeromeP .

On the other hand, I'm not sure I understand everything, how do you see that it's a milling?

Basically, I don't understand everything in this part:

If swCurve.IsCircle Then
            swCurve.GetEndParams Empty, Empty, IsClosed, Empty
            If IsClosed Then
                CurveParam = swCurve.CircleParams
                pos = ""
                If Round(CurveParam(3), 3) = 0 Then pos = pos & Round(CurveParam(0), 3) & "-"
                If Round(CurveParam(4), 3) = 0 Then pos = pos & Round(CurveParam(1), 3) & "-"
                If Round(CurveParam(5), 3) = 0 Then pos = pos & Round(CurveParam(2), 3) & "-"
                Debug.Print pos
                Set Cercle = New Class1
                Cercle.pos = pos
                Cercle.dia = Round(CurveParam(6), 3)
                Set Cercle.edge = swEdge
                Cercles.Add Cercle

 

1 Like

In this part I record the position (pos) and the diameter (CurveParam(6)) of each circle + the circle itself (swEdge).

Since the position is in xyz, I eliminate the height component using the direction of the axis of the circle (CurveParam(3), CurveParam(4), CurveParam(5))

This then allows me to find the circles of the same position, find the largest circle, and hide it.

 

2 Likes

I had understood well for the largest circle, but I didn't understand how you identified 2 circles with the same function-> by the position.

Not stupid at all!

In any case thank you, I don't have to add this code to mine to perfect my personal macro of automatic plan cleaning.

1 Like

After a few tests on the macro, I just noticed that on this type of coin, it hides circle 1 from me instead of only hiding circle 2.

For now that gets around the problem by adding:

If Circle.pos = myCircle.pos And Circle.dia < myCircle.dia And myCircle.dia * 1000 < 38 Then

Because 37mm is the Ø of the M20 countersunk screw which is the largest used. Would there be another solution to avoid this problem, not to take into account the circle if it is part of the outer contour or in relation to the name... (Bearing in mind that the rooms are not often circular.)

1 Like

You can compare the diameter with the dimensions of the bounding box obtained with:

Function GetPreciseBoundingBox(part As SldWorks.PartDoc) As Variant
    Dim dBox(5) As Double
    Dim vBodies As Variant
    vBodies = part.GetBodies2(swBodyType_e.swSolidBody, True)
    Dim minX As Double
    Dim minY As Double
    Dim minZ As Double
    Dim maxX As Double
    Dim maxY As Double
    Dim maxZ As Double
    If Not IsEmpty(vBodies) Then
        Dim i As Integer
        For i = 0 To UBound(vBodies)
            Dim swBody As SldWorks.Body2
            Set swBody = vBodies(i)
            Dim x As Double
            Dim y As Double
            Dim z As Double
            swBody.GetExtremePoint 1, 0, 0, x, y, z
            If i = 0 Or x > maxX Then maxX = x

            swBody.GetExtremePoint -1, 0, 0, x, y, z
            If i = 0 Or x < minX Then minX = x

            swBody.GetExtremePoint 0, 1, 0, x, y, z
            If i = 0 Or y > maxY Then maxY = y

            swBody.GetExtremePoint 0, -1, 0, x, y, z
            If i = 0 Or y < minY Then minY = y

            swBody.GetExtremePoint 0, 0, 1, x, y, z
            If i = 0 Or z > maxZ Then maxZ = z
            
            swBody.GetExtremePoint 0, 0, -1, x, y, z
            If i = 0 Or z < minZ Then minZ = z
        Next
    End If
    dBox(0) = minX: dBox(1) = minY: dBox(2) = minZ
    dBox(3) = maxX: dBox(4) = maxY: dBox(5) = maxZ
    GetPreciseBoundingBox = dBox
End Function

 

1 Like

To call my GetPreciseBoundingBox function in the previous macro, do I have to start from the part and not from the drawing?

 

1 Like

yes you have to apply the function on the part

1 Like

Hello

The code functions perfectly from my macro, on the other hand I can't get the values in my macro or I call the function.

I imagine that the data is recorded in an array but how do I recall the values from my macro?

Option Explicit
Sub main()
Dim swapp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swDraw As SldWorks.DrawingDoc
Dim swView As SldWorks.View
Dim vComps As Variant
Dim vComp As Variant
Dim Comp As SldWorks.Component2
Dim vEdges As Variant
Dim vEdge As Variant
Dim swEdge As SldWorks.edge
Dim swCurve As SldWorks.Curve
Dim IsClosed As Boolean
Dim pos As String
Dim CurveParam As Variant
Dim Cercle As Class1
Dim monCercle As Class1
Dim Cercles As Collection
Set swapp = Application.SldWorks
Set swModel = swapp.ActiveDoc
Set swDraw = swModel
Set swView = swDraw.ActiveDrawingView
If swView Is Nothing Then
    MsgBox "Selectionner une vue"
    Exit Sub
End If

'Ajout SD pour récuperer les valeures de la Bounding Box
Dim swPartModel As SldWorks.ModelDoc2
Set swPartModel = swView.ReferencedDocument
GetPreciseBoundingBox swPartModel
' Fin de l'ajout


Set Cercles = New Collection
vComps = swView.GetVisibleComponents
For Each vComp In vComps
    Set Comp = vComp
    vEdges = swView.GetVisibleEntities(Comp, swViewEntityType_e.swViewEntityType_Edge)
    For Each vEdge In vEdges
        Set swEdge = vEdge
        Set swCurve = swEdge.GetCurve
        If swCurve.IsCircle Then
            swCurve.GetEndParams Empty, Empty, IsClosed, Empty
            If IsClosed Then
                CurveParam = swCurve.CircleParams
                pos = ""
                If Round(CurveParam(3), 3) = 0 Then pos = pos & Round(CurveParam(0), 3) & "-"
                If Round(CurveParam(4), 3) = 0 Then pos = pos & Round(CurveParam(1), 3) & "-"
                If Round(CurveParam(5), 3) = 0 Then pos = pos & Round(CurveParam(2), 3) & "-"
                Debug.Print pos
                Set Cercle = New Class1
                Cercle.pos = pos
                Cercle.dia = Round(CurveParam(6), 3)
                Set Cercle.edge = swEdge
                Cercles.Add Cercle
            End If
        End If
    Next
Next
swModel.ClearSelection2 True
For Each monCercle In Cercles
    For Each Cercle In Cercles
    
        Debug.Print Cercle.dia * 1000
        If Cercle.pos = monCercle.pos And Cercle.dia < monCercle.dia And monCercle.dia * 1000 < 38 Then
          monCercle.edge.Select4 True, Nothing
        End If
    Next
Next
'Modif SD - on ne cache plus temporairement
'swDraw.HideEdge
swModel.ForceRebuild3 False
'swModel.SetLineColor 255 '
swModel.ClearSelection2 True
End Sub

Function GetPreciseBoundingBox(part As SldWorks.PartDoc) As Variant
    Dim dBox(5) As Double
    Dim vBodies As Variant
    vBodies = part.GetBodies2(swBodyType_e.swSolidBody, True)
    Dim minX As Double
    Dim minY As Double
    Dim minZ As Double
    Dim maxX As Double
    Dim maxY As Double
    Dim maxZ As Double
    If Not IsEmpty(vBodies) Then
        Dim i As Integer
        For i = 0 To UBound(vBodies)
            Dim swBody As SldWorks.Body2
            Set swBody = vBodies(i)
            Dim x As Double
            Dim y As Double
            Dim z As Double
            swBody.GetExtremePoint 1, 0, 0, x, y, z
            If i = 0 Or x > maxX Then maxX = x

            swBody.GetExtremePoint -1, 0, 0, x, y, z
            If i = 0 Or x < minX Then minX = x

            swBody.GetExtremePoint 0, 1, 0, x, y, z
            If i = 0 Or y > maxY Then maxY = y

            swBody.GetExtremePoint 0, -1, 0, x, y, z
            If i = 0 Or y < minY Then minY = y

            swBody.GetExtremePoint 0, 0, 1, x, y, z
            If i = 0 Or z > maxZ Then maxZ = z
            
            swBody.GetExtremePoint 0, 0, -1, x, y, z
            If i = 0 Or z < minZ Then minZ = z
        Next
    End If
    dBox(0) = minX: dBox(1) = minY: dBox(2) = minZ
    dBox(3) = maxX: dBox(4) = maxY: dBox(5) = maxZ
        Debug.Print "minX" & minX
    
    
    Debug.Print "minY" & minY
    Debug.Print "minZ" & minZ
    Debug.Print "maxX" & maxX
    Debug.Print "maxY" & maxY
    Debug.Print "maxZ" & maxZ

    
    GetPreciseBoundingBox = dBox
    
    Debug.Print "essai" & dBox(0)
End Function