n° 2 automatic constrains

n° 2 automatic constrains

TecnicoPanni
Participant Participant
453 Views
8 Replies
Message 1 of 9

n° 2 automatic constrains

TecnicoPanni
Participant
Participant

Good morning,

I have this macro, which automatically creates a constraint between the axis Z of the assembly and the axis Z of the selected part.

 

Sub Vincola_AsseZ()


Dim oAsmCompDef As AssemblyComponentDefinition
Set oAsmCompDef = ThisApplication.ActiveDocument.ComponentDefinition

Dim oComp As ComponentOccurrence

' Get the XY plane from each occurrence. This goes to the
' component definition of the part to get this information.
' This is the same as accessing the part document directly.
' The work plane obtained is in the context of the part,
' not the assembly.

Dim oCompPlane1 As WorkPlane
Dim oCompAxisZ As WorkAxis

' set the axis Z of the active assembly
Dim AsmAxisZ As WorkAxis
Set AsmAxisZ = oAsmCompDef.WorkAxes.Item(3)

selezione:

'reference to the selected dimension
Set oComp = ThisApplication.CommandManager.Pick(kAssemblyOccurrenceFilter, "Seleziona il componente")
'exit sub if is nothing (Esc)
If oComp Is Nothing Then Exit Sub

Set oCompAxisZ = oComp.Definition.WorkAxes.Item(3)


' Because we need the work plane in the context of the assembly
' we need to create proxies for the work planes. The proxies
' represent the work planes in the context of the assembly.

Dim oAsmAxisZ As WorkAxisProxy
Call oComp.CreateGeometryProxy(oCompAxisZ, oAsmAxisZ)



' Create the constraint using the work plane proxies.

Call oAsmCompDef.Constraints.AddMateConstraint(oAsmAxisZ, AsmAxisZ, 0) ' .AddMateConstraint(oAsmPlane1, oAsmPlane2, 0)

 


'loop selection
GoTo selezione

End Sub

 

I would like to automatically add a second constraint: angle constrain between the axis Y of the assembly and the YZ plane of the same selected part.

 

I've tried by myself, but without success...
...does anyone can help me?

 

Many thanks

0 Likes
Accepted solutions (1)
454 Views
8 Replies
Replies (8)
Message 2 of 9

FINET_Laurent
Advisor
Advisor

Hi @TecnicoPanni,

 

What would be the angle value ?

 

Kind regards,

FINET L.

If this post solved your question, please kindly mark it as "Solution"

If this post helped out in any way to solve your question, please drop a "Like"

@LinkedIn     @JohnCockerill

0 Likes
Message 3 of 9

TecnicoPanni
Participant
Participant

as default, 0

0 Likes
Message 4 of 9

dypro
Advocate
Advocate

hi @FINET_Laurent @TecnicoPanni 
If there is a way to do it, I would be interested as well

0 Likes
Message 5 of 9

m_baczewski
Advocate
Advocate
Accepted solution

Hello  @TecnicoPanni

Try to use this code, in this code you can insert your own angle in degree. Give feedback if its correct. 

 

Sub Vincola_AsseZ()

Dim angle As Integer
angle = 0

Dim rad As Double
rad = (angle * (4 * Math.Atn(1))) / 180

Dim oAsmCompDef As AssemblyComponentDefinition
Set oAsmCompDef = ThisApplication.ActiveDocument.ComponentDefinition

Dim oComp As ComponentOccurrence

' Get the XY plane from each occurrence. This goes to the
' component definition of the part to get this information.
' This is the same as accessing the part document directly.
' The work plane obtained is in the context of the part,
' not the assembly.

Dim oCompAxisZ As WorkAxis
Dim oCompPlaneYZ As WorkPlane

' set the axis Z of the active assembly
Dim AsmAxisZ As WorkAxis
Dim AsmAxisY As WorkAxis
Set AsmAxisZ = oAsmCompDef.WorkAxes.Item(3)
Set AsmAxisY = oAsmCompDef.WorkAxes.Item(2)


selezione:

'reference to the selected dimension
Set oComp = ThisApplication.CommandManager.Pick(kAssemblyOccurrenceFilter, "Seleziona il componente")
'exit sub if is nothing (Esc)
If oComp Is Nothing Then Exit Sub

Set oCompAxisZ = oComp.Definition.WorkAxes.Item(3)
Set oCompPlaneYZ = oComp.Definition.WorkPlanes.Item(1)
    
' Because we need the work plane in the context of the assembly
' we need to create proxies for the work planes. The proxies
' represent the work planes in the context of the assembly.

Dim oAsmAxisZ As WorkAxisProxy
Call oComp.CreateGeometryProxy(oCompAxisZ, oAsmAxisZ)

Dim oAsmPlaneYZ As WorkPlaneProxy
Call oComp.CreateGeometryProxy(oCompPlaneYZ, oAsmPlaneYZ)

' Create the constraint using the work plane proxies.

Call oAsmCompDef.Constraints.AddMateConstraint(oAsmAxisZ, AsmAxisZ, 0) ' .AddMateConstraint(oAsmPlane1, oAsmPlane2, 0)
Call oAsmCompDef.Constraints.AddAngleConstraint(oAsmPlaneYZ, AsmAxisY, rad)

'loop selection
GoTo selezione

End Sub

 

 

Message 6 of 9

dypro
Advocate
Advocate

hello @m_baczewski 

 

I'm trying to use the code as well but in my case is in ilogic
I have deleted the "set" and the previous version of the code worked, but for some reason I get an error in this line:

	rad = (angle * (4 * Math.Atn(1))) / 180

 "Atn" is not a member of "System.Math"

 

Is there anyway to make it work in my case?

0 Likes
Message 7 of 9

m_baczewski
Advocate
Advocate

Hello @dypro 

 

You sholud change this line of code to looks like:

 

rad = (angle * Math.PI) / 180

 

Message 8 of 9

dypro
Advocate
Advocate

Thanks @m_baczewski 

 

I just changed the 

rad = (angle * (4 * Math.Atn(1))) / 180

to 

rad = (angle * (4 * Math.Atan(1))) / 180

 and it worked well so thankyou

0 Likes
Message 9 of 9

m_baczewski
Advocate
Advocate
I'm glad you managed to solve this problem.
0 Likes