Examinig part component mates ?

Hi there,

I have a part that is mated with a line in a 3D-Sketch. The3D-Sketch is in an assembly. The user selects the part on thescreen and my addin recieves the IComponent2 interface of the part.

How do I get the ISketchLine-Interface of the mated line?

My idea was to examine the mates of the Part by examining thefeature tree of the part. Unfortunately the functions that returnsthe feature tree of the component returns everything except theMateGroup of the part.
I use the same functions that were descibed in the SolidWorks APIHelp "Traversing Assembly at Component and Feature Levels Example"

Thanks in advance,

Matthias SenffSolidworksApi macros
Answers 10
JB
JB 2009-01-28 Best Answer
Although the feature tree of an assembly appears to show themates of a part inside a folder under the part, I'm pretty surethey really aren't there. I think you'll have to traverse the matesof the assembly and check their components to see if one of thecomponents is the one you are interested in.
SF
SF 2009-01-28
Unfortunately our solution will work with a lot of parts,assemblys and sub assemblys. If we traverse all features of allassemblys and sub assemblys it will lead to performanceproblems.
DM
DM 2009-01-28
When you traverse the assembly your program does not followthe manager tree so I think it would be hard to find the mates foreach part. To follow the tree the way it shows in your window,check out the "Traverse FeatureManager Design Tree" example and"TreeControlItem." This goes down the tree in the same order thatthe tree is in. It uses nodes like the tree view in VB. Dependingon how you have your SW Tree Options set ( See below), when youfind a part or an assembly in the tree, then the next "childnode"will either be the mates for that part or the folder that yourmates are in. You can then get each mates using childnode.getnextand examine each mate. A couple commands you will learn if youdon't know them already will be Feat.GetSpecificFeature2,Mate.MateEntity and MateEnt.Reference.
To set your tree options, right click on the top feature in youtree, which is the name of your part, choose "Tree Display" andthen either "View Features" or "View Mates and Dependencies" thiswill change whether the next item below the part in the tree is afolder or a mate.
I hope this isn't to long of a note.
Hope this helps.

Dan Miel
IK
IK 2009-01-29
you can begin examine your selected part and go upperwards.It is not necessary traverse all components.

Option Explicit


Sub main()
Dim swModel As SldWorks.ModelDoc2
Dim swApp As SldWorks.SldWorks
Dim swFeat As SldWorks.Feature
Dim swMateFeat As SldWorks.Feature
Dim swSubFeat As SldWorks.Feature
Dim swMate As SldWorks.Mate2
Dim swSelMgr As SldWorks.SelectionMgr
Dim comp As SldWorks.Component2
Dim Model1 As SldWorks.ModelDoc2

Set swApp = Application.SldWorks
If swApp Is Nothing Then
Err.Raise 1234, , "Solidworks is gone."
End If

Set swModel = swApp.ActiveDoc
Set swSelMgr = swModel.SelectionManager
Set comp = swSelMgr.GetSelectedObjectsComponent(1)
If comp Is Nothing Then
Set Model1 = swModel
Else
Set Model1 = comp.GetModelDoc
End If
Set swFeat = Model1.FirstFeature

back:
' Iterate over features in FeatureManager design tree
Do While Not swFeat Is Nothing
If UCase("MateGroup") = UCase(swFeat.GetTypeName) Then
Set swMateFeat = swFeat
Exit Do
End If
Set swFeat = swFeat.GetNextFeature
Loop

If Not swMateFeat Is Nothing Then ' Get first mate, which is asubfeature
Set swSubFeat = swMateFeat.GetFirstSubFeature
Do While Not swSubFeat Is Nothing
Set swMate = swSubFeat.GetSpecificFeature2
If Not swMate Is Nothing Then
Debug.Print swMate.MateEntity(0).ReferenceComponent.Name2
Debug.Print swMate.MateEntity(1).ReferenceComponent.Name2
End If

' Get the next mate in MateGroup
Set swSubFeat = swSubFeat.GetNextSubFeature
Loop
Else
If Not comp Is Nothing Then
Set comp = comp.GetParent
If comp Is Nothing Then
Set Model1 = swModel
Else
Set Model1 = comp.GetModelDoc
End If
Set swFeat = Model1.FirstFeature
If Not swFeat Is Nothing Then GoTo back
End If
End If

End Sub

SF
SF 2009-01-29
I thought there is an easier way. Examining only the parentswill not help, because the 3D-Sketch may be in a subassembly of aparent. I think I will check out traversing the FeatureManagerDesign Tree.

Thanks for the answers.

Matthias Senff

IK
IK 2009-01-29
but mate is allways in parent.

set comp = swMate.MateEntity(0).ReferenceComponent you can findcomponent Set ent = swMateEnt(0).Reference you have entity usingfor mate.
SF
SF 2009-01-29
Thanks again,

I was unaware that SolidWorks assigns the Mates automatically tothe correct assembly if you change the underlying structures sothat you can always reach them by the parents.
DM
DM 2009-01-29
If you replace a part in an assembly and the new part doesnot have the same surface as the old part, or if a component issuppressed the MateEntity will not return the ReferenceComponent.So if you select a mate and it has an invalid component, when youcheck for the ReferenceComponent, it will not return a name becausethe link is broken.

When you traverse the FeatureManager Design tree and check themates below each part and assembly, you know which mates belong tothat part. You can then check each mate for broken references.

What I'm saying is that as long as you know that your mates aregood and not invalid you can find the components for the mate likeIvana is doing, but if a ReferenceComponent is suppressed then Idon't think MateEntity will find it.

Dan Miel
DM
DM 2009-02-02
Matthias
This macro should go down the tree and find each component and thenthe mates for that component. It retrieves the information whetherthe components are in folders or not. I wrote this macro so itfinds all parts of all sub assemblies. If you want it to find thetop level only, comment out the line indicated. This prints out thecomponent name the mates to that component and any folder that thecomponents are in.
If you are looking for mates for a particular part you just need toinsert a check that looks for the component name that you want tocheck.

Dan Miel
SW 2008
SF
SF 2009-02-09
Thanks Dan,

I try to combine your and Ivanas code to find an efficientsolution.

Matthias Senff