VBA - Een variabele een handmatige selectie toewijzen

Hallo

Uit een selectie zoals deze code

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

Ik wil een eerste bewerking uitvoeren en in plaats van opnieuw een selectie te maken om naar hetzelfde vlak te wijzen, is het mogelijk om het resultaat van het aanwijzen in een variabele te plaatsen? En hoe noem je deze selectie om de nieuwe bewerking uit te voeren?

Ik hoop dat ik duidelijk genoeg ben geweest...

Hallo

"Is het mogelijk om het resultaat van de aanwijzer in een variabele te plaatsen?": dit is al wat je doet aangezien je je selectie opslaat in de swEnt-variabele, dus zolang je macro actief is, reset je deze variabele niet of wijs je er een nieuwe waarde aan toe ...

Vriendelijke groeten

Ja, ik ben ok met het principe, maar hoe wijs ik daarna naar deze variabele? (zie opmerking aan het einde van de code)

​
 

'*****************************************************
'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)

 

​

 

Hallo;

Eén aanwijzing  = https://help.solidworks.com/SOLIDWORKS.Interop.sldworks~SOLIDWORKS.Interop.sldworks.IFeatureManager~InsertRefPlane.html
met

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

Voordat u deze methode aanroept, moet u de referentie-entiteiten hebben geselecteerd met behulp van deze markeringen met IModelDocExtension::SelectByID2:

0 = Eerste referentie-entiteit
1 = Tweede referentie-entiteit
2 = Derde referentie-entiteit


Vriendelijke groeten
 

 

Als ik je aanwijzing begrijp, moet ik dit doen

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

 

Wat plaats ik in plaats van ????

Hallo

Om het gezicht te selecteren, kunt u het volgende gebruiken:

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 like

Hallo;

de functie "InsertRefPlane" neemt automatisch de vorige selecties op volgorde.
Behalve voor het gemak is het niet nodig om ze te benoemen. je hoeft alleen maar "InsertRefPlane" te vertellen wat je ermee wilt doen:

Bijvoorbeeld: de macro hieronder

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

Gelijk aan:
Creatie van een nieuw vlak: Samenvallen (4) en (0) met mijn eerste selectie (Ref1) met een hoek van 80° (16) en (1.39626340159547) met mijn tweede selectie...
In jouw geval InsertRefPlane(32,0,2,0,0,0) = tangent aan de eerste selectie en dan loodrecht op de tweede selectie.

De waarden die het type beperkingen bepalen, worden hier vermeld help.solidworks.com

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

Vriendelijke groeten

1 like

  @Maclane @JeromeP 

Dank u voor uw antwoorden, ik zal er vanavond naar kijken. Ik had een druk weekend achter de rug om weer in mijn ontwikkeling te duiken. Ik hou jullie toch op de hoogte

Hallo

Aangezien swEnt een gezicht is, kun je het ophalen met behulp van de IsSame-functie op het moment dat je het nodig hebt. Wees voorzichtig, het 3D-model mag niet opnieuw worden opgebouwd voordat deze functie wordt aangeroepen, anders werkt het niet (geef de voorkeur aan het gebruik van swModel.GraphicsRedraw2 indien nodig).

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

Een andere oplossing zou zijn om dit gezicht een naam te geven:

    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)

Zoek er dan naar op zijn kleine naam wanneer je het nodig hebt.

    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

Vriendelijke groeten

1 like

Die bijvoorbeeld dit kan geven op de bijlage:

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 like

@d.Roger respect!

Ik loop overdag met mijn nieuwe vaste contract en 's avonds met mijn voormalige klanten die me niet loslaten (ik klaag niet, hè) en je geeft me een hele ontwikkeling op mijn probleem!

Ik zal echt tijd moeten vrijmaken om hiernaar te kijken, in ieder geval om uw werk te eren en in het verlengde daarvan aan al diegenen die door hun antwoorden de schmilblick vooruit helpen!