VBA - Przypisywanie zmiennej do wyboru ręcznego

Witam

Z wyboru, takiego jak ten kod

Dim swEnt As SldWorks.Face2
Set swEnt = Part.SelectionManager.GetSelectedObject6(1, 0)
Do While swEnt Is Nothing
    DoEvents
    Set swEnt = Part.SelectionManager.GetSelectedObject6(1, 0)
Loop

Chcę wykonać pierwszą operację i czy zamiast ponownie dokonywać wyboru w celu wskazania tej samej powierzchni, możliwe jest umieszczenie wyniku wskazywania w zmiennej? A jak wywołać ten wybór, aby wykonać nową operację?

Mam nadzieję, że wyraziłem się wystarczająco jasno...

Witam

"Czy możliwe jest umieszczenie wyniku wskazywania w zmiennej?": to jest już to, co robisz, ponieważ przechowujesz swój wybór w zmiennej swEnt, więc dopóki twoje makro jest uruchomione, nie resetujesz tej zmiennej ani nie przypisujesz jej nowej wartości ...

Pozdrowienia

Tak, nie mam nic przeciwko tej zasadzie, ale jak mogę później wskazać na tę zmienną? (patrz komentarz na końcu kodu)

​
 

'*****************************************************
'Axe extrusion
'*****************************************************

Dim swEnt As SldWorks.Face2
Set swEnt = Part.SelectionManager.GetSelectedObject6(1, 0)
Do While swEnt Is Nothing
    DoEvents
    Set swEnt = Part.SelectionManager.GetSelectedObject6(1, 0)
Loop

boolstatus = Part.InsertAxis2(True)

'*****************************************************
'Plan 2 axes
'*****************************************************

boolstatus = Part.Extension.SelectByID2("Axe1", "AXIS", 0, 0, 0, True, 1, Nothing, 0)

Dim myRefPlane As Object
Set myRefPlane = Part.FeatureManager.InsertRefPlane(4, 0, 4, 0, 0, 0)

'*****************************************************
'Plan perpenticulaire et tangent
'*****************************************************

'Comment je récupère la valeur de swEnt pour réaliser mon plan tangent à swEnt et perpendiculaire au plan créer juste au dessus ?

Set myRefPlane = Part.FeatureManager.InsertRefPlane(2, 0, 32, 0, 0, 0)

 

​

 

Witam;

Jedna wskazówka  = https://help.solidworks.com/SOLIDWORKS.Interop.sldworks~SOLIDWORKS.Interop.sldworks.IFeatureManager~InsertRefPlane.html
z

. InsertRefPlane(FirstConstraint, FirstConstraintAngleOrDistance_
, SecondConstraint, SecondConstraintAngleOrDistance, ThirdConstraint, ThirdConstraintAngleOrDistance)
i

Przed wywołaniem tej metody należy wybrać jednostki referencyjne przy użyciu tych znaczników z IModelDocExtension::SelectByID2:

0 = Pierwszy element odniesienia
1 = Drugi element odniesienia
2 = Trzecia jednostka referencyjna


Pozdrowienia
 

 

Jeśli dobrze rozumiem twoją wskazówkę, muszę to zrobić

boolstatus = Part.Extension.SelectByID2(swEnt, "????", 0, 0, 0, True, 1, Nothing, 0)

 

Co mam umieścić zamiast ????

Witam

Aby wybrać twarz, możesz użyć:

Dim swEntity As SldWorks.entity
Set swEntity = swEnt
Dim swSelectData As SldWorks.SelectData
Set swSelectData = swSelectionMgr.CreateSelectData
swSelectData.mark = 1
swEntity.Select4 True, swSelectData

 

1 polubienie

Witam;

Funkcja "InsertRefPlane" automatycznie pobiera poprzednie wybory w odpowiedniej kolejności.
Z wyjątkiem wygody nie trzeba ich wymieniać. musisz tylko powiedzieć "InsertRefPlane", co chcesz z nim zrobić:

Na przykład: makro poniżej

Dim swApp As Object

Dim Part As Object
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long

Sub main()

Set swApp = Application.SldWorks

Set Part = swApp.ActiveDoc
Dim myModelView As Object

Part.ClearSelection2 True

Set swModelView = Part.ActiveView
Ref1 = Part.Extension.SelectByRay(7.22190712104975E-02, 9.98370992863329E-03, 0.304415424010585, -0.774421370181801, -0.406469481117021, -0.484823784818983, 1.56729303225411E-03, 1, True, 0, 0)
ref2 = Part.Extension.SelectByRay(-7.86713451373089E-03, -6.76744786841255E-02, 0.304799999999886, -0.774421370181801, -0.406469481117021, -0.484823784818983, 1.56729303225411E-03, 2, True, 1, 0)
Dim myRefPlane As Object
Set myRefPlane = Part.FeatureManager.InsertRefPlane(4, 0, 16, 1.39626340159547, 0, 0)

Part.ClearSelection2 True
End Sub

Równoważny:
Utworzenie nowej płaszczyzny: Zbieżne (4) i (0) z moim pierwszym wyborem (Ref1) o kącie 80° (16) i (1.39626340159547) z moim drugim wyborem...
W Twoim przypadku InsertRefPlane(32,0,2,0,0,0) = styczna do pierwszego wyboru, a następnie prostopadła do drugiej selekcji.

Wartości określające typ wiązań są wymienione tutaj help.solidworks.com

swRefPlaneReferenceConstraint_Angle16 lub 0x10
swRefPlaneReferenceConstraint_Coincident4 lub 0x4
swRefPlaneReferenceConstraint_Distance8 lub 0x8
swRefPlaneReferenceConstraint_MidPlane128 lub 0x80
swRefPlaneReferenceConstraint_OptionFlip256 lub 0x100
swRefPlaneReferenceConstraint_OptionOriginOnCurve512 lub 0x200
swRefPlaneReferenceConstraint_OptionProjectAlongSketchNormal2056 lub 0x800
swRefPlaneReferenceConstraint_OptionProjectToNearestLocation1028 lub 0x400
swRefPlaneReferenceConstraint_OptionReferenceFlip8192 Złoto 0x2000
swRefPlaneReferenceConstraint_Parallel1 lub 0x1
swRefPlaneReferenceConstraint_ParallelToScreen4096 lub 0x1000 
swRefPlaneReferenceConstraint_Perpendicular2 lub 0x2
swRefPlaneReferenceConstraint_Project64 lub 0x40
swRefPlaneReferenceConstraint_Tangent32 lub 0x20

Pozdrowienia

1 polubienie

  @Maclane @JeromeP 

Dziękuję za odpowiedzi, przyjrzę się temu dziś wieczorem. Miałem pracowity weekend, aby ponownie zanurzyć się w moim rozwoju. I tak będę Cię informować

Witam

Ponieważ swEnt jest twarzą , możesz ją pobrać za pomocą funkcji IsSame w momencie, gdy jej potrzebujesz. Bądź ostrożny, model 3D nie może być przebudowywany przed wywołaniem tej funkcji, w przeciwnym razie nie będzie działać (w razie potrzeby preferuj użycie swModel.GraphicsRedraw2).

Set swSelData = swModel.SelectionManager.CreateSelectData
Set swPart = swModel
vBodies = swPart.GetBodies2(swAllBodies, True)
Set swBody = vBodies(0)
Set swFace = swBody.GetFirstFace
Do While Not swFace Is Nothing
   status = swFace.IsSame(swEnt)
   If status Then
       swFace.Select4 True, swSelData
       Exit Do
   End If
   Set swFace = swFace.GetNextFace
Loop

Innym rozwiązaniem byłoby nazwanie tej twarzy:

    Set swEnt = swModel.SelectionManager.GetSelectedObject6(1, -1)
    Do While swEnt Is Nothing
        DoEvents
        Set swEnt = swModel.SelectionManager.GetSelectedObject6(1, -1)
    Loop

    faceName = "MaFace"

    Set swBody = swEnt.GetBody

    BodyName = swBody.Name

    status = swModel.SelectedFaceProperties(0, 0, 0, 0, 0, 0, 0, True, faceName)

Następnie wyszukaj go według jego małej nazwy, gdy go potrzebujesz.

    Set swSelData = swModel.SelectionManager.CreateSelectData
    Set swPart = swModel
    vBodies = swPart.GetBodies2(swAllBodies, True)
    Set swBody = vBodies(0)
    Set swFace = swBody.GetFirstFace
    Do While Not swFace Is Nothing
        currentFaceName = swModel.GetEntityName(swFace)
        If (currentFaceName = faceName) Then
            swFace.Select4 True, swSelData
            Exit Do
        End If
        Set swFace = swFace.GetNextFace
    Loop

Pozdrowienia

1 polubienie

Który na przykład może podać to na załączniku:

Option Explicit

Dim swApp As SldWorks.SldWorks
Dim swModel As ModelDoc2
Dim swPart As PartDoc
Dim swBody As SldWorks.Body2
Dim vBodies As Variant
Dim theFeature As SldWorks.Feature
Dim swFeature As SldWorks.Feature
Dim myFeature As SldWorks.Feature
Dim skSegment As SldWorks.SketchSegment
Dim myRefPlane As SldWorks.RefPlane
Dim swEnt As SldWorks.Face2
Dim swFace As SldWorks.Face2
Dim swSelData As SldWorks.SelectData
Dim status As Boolean
Dim faceName As String
Dim BodyName As String
Dim AxeName As String
Dim PlanName As String
Dim currentFaceName As String
Dim featCount As Long
Dim featName As String
Dim i As Long

Sub main()
    On Error GoTo Handler

    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc

    swModel.ClearSelection2 True

    Set swEnt = swModel.SelectionManager.GetSelectedObject6(1, -1)
    Do While swEnt Is Nothing
        DoEvents
        Set swEnt = swModel.SelectionManager.GetSelectedObject6(1, -1)
    Loop

    status = swModel.InsertAxis2(True)
    
    If status = False Then
        MsgBox "Il n'est pas possible de créer un axe sur cette sélection."
        swModel.ClearSelection2 True
        Exit Sub
    End If

    swModel.GraphicsRedraw2
    swModel.ClearSelection2 True

    featCount = swModel.GetFeatureCount
    Set theFeature = swModel.FeatureByPositionReverse(0)
    If Not theFeature Is Nothing Then
        featName = theFeature.Name
    End If

    AxeName = "MonAxe"
    status = swModel.Extension.SelectByID2(featName, "AXIS", 0, 0, 0, False, 0, Nothing, 0)
    status = swModel.SelectedFeatureProperties(0, 0, 0, 0, 0, 0, 0, True, False, AxeName)
    i = 0
    Do While status = False
        i = i + 1
        AxeName = "MonAxe" & i
        status = swModel.SelectedFeatureProperties(0, 0, 0, 0, 0, 0, 0, True, False, AxeName)
    Loop

    swModel.GraphicsRedraw2
    swModel.ClearSelection2 True

    '************************************************************
    'Ajout d'un usinage pour test
    '************************************************************
    status = swModel.Extension.SelectByID2("Plan de droite", "PLANE", 0, 0, 0, False, 0, Nothing, 0)
    swModel.SketchManager.InsertSketch True
    Set skSegment = swModel.SketchManager.CreateCircle(-0.04, 0#, 0#, -0.03, 0.01, 0#)
    
    swModel.ViewOrientationUndo
    
    Set myFeature = swModel.FeatureManager.FeatureCut4(False, False, False, 9, 1, 0.001, 0.001, False, False, False, False, 0, 0, False, False, False, False, False, True, True, True, True, False, 0, 0, False, False)
    swModel.SelectionManager.EnableContourSelection = False
    
    swModel.GraphicsRedraw2
    swModel.ClearSelection2 True
    
    If myFeature Is Nothing Then
        MsgBox "Usinage impossible : la géométrie ne croise pas le modèle."
        swModel.EditUndo2 2
    End If

    '************************************************************
    'Plan coincident 1 axe et perpendiculaire 1 plan de référence
    '************************************************************
    status = swModel.Extension.SelectByID2(AxeName, "AXIS", 0, 0, 0, True, 0, Nothing, 0)
    status = swModel.Extension.SelectByID2("Plan de dessus", "PLANE", 0, 0, 0, True, 1, Nothing, 0)

    Set myRefPlane = swModel.FeatureManager.InsertRefPlane(4, 0, 2, 0, 0, 0)

    swModel.GraphicsRedraw2
    swModel.ClearSelection2 True

    featCount = swModel.GetFeatureCount
    Set theFeature = swModel.FeatureByPositionReverse(0)
    If Not theFeature Is Nothing Then
        featName = theFeature.Name
    End If

    PlanName = "MonPlan"
    status = swModel.Extension.SelectByID2(featName, "PLANE", 0, 0, 0, False, 0, Nothing, 0)
    status = swModel.SelectedFeatureProperties(0, 0, 0, 0, 0, 0, 0, True, False, PlanName)
    i = 0
    Do While status = False
        i = i + 1
        PlanName = "MonPlan" & i
        status = swModel.SelectedFeatureProperties(0, 0, 0, 0, 0, 0, 0, True, False, PlanName)
    Loop
    
    swModel.GraphicsRedraw2
    swModel.ClearSelection2 True

    '****************************************************
    'Plan tangent à ma face et perpenticulaire à mon plan
    '****************************************************
    Set swSelData = swModel.SelectionManager.CreateSelectData
    Set swPart = swModel
    vBodies = swPart.GetBodies2(swAllBodies, True)
    Set swBody = vBodies(0)
    Set swFace = swBody.GetFirstFace
    Do While Not swFace Is Nothing
        status = swFace.IsSame(swEnt)
        If status Then
            swFace.Select4 True, swSelData
            Exit Do
        End If
        Set swFace = swFace.GetNextFace
    Loop
    
    status = swModel.Extension.SelectByID2(PlanName, "PLANE", 0, 0, 0, True, 1, Nothing, 0)

    Set myRefPlane = swModel.FeatureManager.InsertRefPlane(32, 0, 2, 0, 0, 0)

    swModel.GraphicsRedraw2
    swModel.ClearSelection2 True
    
    featCount = swModel.GetFeatureCount
    Set theFeature = swModel.FeatureByPositionReverse(0)
    If Not theFeature Is Nothing Then
        featName = theFeature.Name
    End If
    
    status = swModel.Extension.SelectByID2(featName, "PLANE", 0, 0, 0, False, 0, Nothing, 0)
    status = swModel.SelectedFeatureProperties(0, 0, 0, 0, 0, 0, 0, True, False, PlanName)
    i = 0
    Do While status = False
        i = i + 1
        PlanName = "MonPlan" & i
        status = swModel.SelectedFeatureProperties(0, 0, 0, 0, 0, 0, 0, True, False, PlanName)
    Loop
    
    swModel.ForceRebuild3 True
    swModel.ClearSelection2 True
    
    MsgBox "Traitement terminé."
    
    Exit Sub
    
Handler:
    MsgBox "Traitement terminé sur erreur."
    swModel.ClearSelection2 True
    Exit Sub
End Sub

 


macroselectface.sldprt
1 polubienie

@d. Szacunek Rogera !

W ciągu dnia biegam z moją nową umową na czas nieokreślony, a wieczorem z moimi byłymi klientami, którzy nie odpuszczają mi (nie narzekam, eh), a ty dajesz mi cały rozwój mojego problemu!

Naprawdę będę musiał poświęcić trochę czasu, aby się temu przyjrzeć, przynajmniej po to, aby uhonorować waszą pracę, a co za tym idzie, wszystkich tych, którzy swoimi odpowiedziami sprawiają, że schmilblick idzie naprzód!