Makro - Ukryj okrąg frezowania (Wspomaganie wiercenia)

Witam

Mam projekt makro, aby ukryć okręgi frezowania na naszych posłach (tylko na 1. arkuszu i 1. widoku)

Wiedząc, że nie zawsze ich nie ma.

1- Czy Twoim zdaniem jest to wykonalne?

2-Mały pomysł na funkcje, które należy rozpocząć? (w szczególności, jak zidentyfikować, czy okrąg jest okręgiem frezowania)

 

Poniżej znajduje się przykład 2 napotkanych rodzajów pogłębiania (luz normalny lub prześwit głowy + dodane komory):

Każdy trop, który może mi pomóc zacząć, będzie mile widziany.

Odkryłem, że koła są w kolorze czerwonym, ale jak wybrać tylko kółka do frezowania?

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

Dziękuję.

1 polubienie

Spróbuj tego:

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

Wstaw > modułu klasy > nazwę: Klasa1

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

 

3 polubienia

Po raz kolejny doskonale funkcjonalny. Jeszcze raz dziękuję JeromeP .

Z drugiej strony nie jestem pewien, czy wszystko rozumiem, jak widzisz, że to mielenie?

W zasadzie nie rozumiem wszystkiego w tej części:

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 polubienie

W tej części zapisuję położenie (pos) i średnicę (CurveParam(6)) każdego okręgu + samego okręgu (swEdge).

 Ponieważ pozycja jest w xyz, eliminuję składową wysokości, używając kierunku osi okręgu (CurveParam(3), CurveParam(4), CurveParam(5))

To z kolei pozwala mi znaleźć okręgi o tej samej pozycji, znaleźć największy okrąg i ukryć go.

 

2 polubienia

Dobrze zrozumiałem największy okrąg, ale nie rozumiałem, jak zidentyfikowałeś 2 okręgi o tej samej funkcji - > przez pozycję.

Wcale nie jest głupi!

W każdym razie dziękuję, nie muszę dodawać tego kodu do mojego, aby udoskonalić moje osobiste makro automatycznego czyszczenia planu.

1 polubienie

Po kilku testach na makrze zauważyłem tylko, że na tego typu monecie ukrywa przede mną kółko 1, a nie tylko kółko 2.

Na razie pozwala to obejść problem, dodając:

Jeśli Circle.pos = myCircle.pos i Circle.dia < myCircle.dia i myCircle.dia * 1000 < 38 to

Ponieważ 37 mm to Ø z stożkowym M20, która jest największą używaną. Czy byłoby inne rozwiązanie, aby uniknąć tego problemu, nie brać pod uwagę okręgu, jeśli jest on częścią zewnętrznego konturu lub w odniesieniu do nazwy... (Mając na uwadze, że pokoje często nie są okrągłe.)

1 polubienie

Średnicę można porównać z wymiarami ramki granicznej uzyskanymi za pomocą:

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 polubienie

Czy aby wywołać funkcję GetPreciseBoundingBox w poprzednim makrze, muszę zacząć od elementu, a nie od rysunku?

 

1 polubienie

Tak, musisz zastosować funkcję na części

1 polubienie

Witam

Kod działa idealnie z mojego makra, z drugiej strony nie mogę uzyskać wartości w moim makrze lub wywołuję funkcję.

Wyobrażam sobie, że dane są zapisane w tablicy, ale jak mogę przypomnieć sobie wartości z mojego makra?

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