Creating Feature Based Pattern

Creating Feature Based Pattern

felix.cortes5K3Y2
Advocate Advocate
808 Views
2 Replies
Message 1 of 3

Creating Feature Based Pattern

felix.cortes5K3Y2
Advocate
Advocate

Hi Forum,

 

I am trying to create a pattern of a part inside an assembly based off a rectangular part feature from another part but can't seem to get the code to work. Here's what I have written so far:

 

 

	
	Dim BrowserName As String = "SERPENTINE SHOE" 'the part that needs to be pattern
	Dim PatternBrowserName As String = BrowserName & "S" 'name of pattern that will be made
	Dim FeatureName As String = "Shoe Pattern" 'name of the feature
	Dim RefPartName As String = "SERPENTINE SPREADER FOR SHOE" 'name of the part with the feature
	
	Dim oAsm As AssemblyDocument = ThisApplication.ActiveDocument
	Dim oAsmComp As AssemblyComponentDefinition = oAsm.ComponentDefinition
	Dim oOcc As ComponentOccurrence
	Dim oOccPattern As OccurrencePattern
	Dim PartExists As Boolean = False
	Dim PatternExists As Boolean = False 
	Dim oPattern As ObjectCollection = ThisApplication.TransientObjects.CreateObjectCollection
	Dim n As Integer = 1
	Dim RefExists As Boolean = False
	Dim oPartDoc As PartDocument
	Dim oPartComp As PartComponentDefinition
	Dim oFeat As PartFeature
	
	For Each oOcc In oAsmComp.Occurrences
		If oOcc.Name = BrowserName Then
			PartExists = True
			oPattern.Add(oAsmComp.Occurrences.Item(n))
		Else If oOcc.Name = RefPartName Then
			RefExists = True
			oPartDoc = ThisApplication.Documents.Open(oOcc.ReferencedDocumentDescriptor.FullDocumentName, False)
			oPartComp = oPartDoc.ComponentDefinition
			oFeat = oPartComp.Features.RectangularPatternFeatures.Item(FeatureName)
		End If 
		n = n + 1
	Next
	
	'[ adds pattern into the assembly
		Dim oOccPtrns As OccurrencePatterns = oAsmComp.OccurrencePatterns
		Dim oPtrn As OccurrencePattern
		oPtrn = oOccPtrns.AddFeatureBasedPattern(oPattern, oFeat)
		oPtrn.Name = PatternBrowserName
	']


Best,

Felix

0 Likes
Accepted solutions (1)
809 Views
2 Replies
Replies (2)
Message 2 of 3

JhoelForshav
Mentor
Mentor
Accepted solution

Hi @felix.cortes5K3Y2 

You'll need to create a proxy for your partfeature in order to use it in the assembly. If you just go through the parts componentdefinition to get this pattern feature, there's no way of knowing which feature to use if there are multiple occurrences of the same part in your assembly. I hope this makes sense. So what you need to do is create a proxy through the specific occurrence containing the feature pattern that you want to use.

 

I've made some minor changes to the code, the main thing though is that it's now creating a proxy object of the partfeature pattern. Hope this helps 🙂

 

Dim BrowserName As String = "SERPENTINE SHOE" 'the part that needs to be pattern
Dim PatternBrowserName As String = BrowserName & "S" 'name of pattern that will be made
Dim FeatureName As String = "Shoe Pattern" 'name of the feature
Dim RefPartName As String = "SERPENTINE SPREADER FOR SHOE" 'name of the part with the feature

Dim oAsm As AssemblyDocument = ThisApplication.ActiveDocument
Dim oAsmComp As AssemblyComponentDefinition = oAsm.ComponentDefinition
Dim oOccPattern As OccurrencePattern
Dim PartExists As Boolean = False
Dim PatternExists As Boolean = False
Dim oPattern As ObjectCollection = ThisApplication.TransientObjects.CreateObjectCollection
Dim RefExists As Boolean = False
Dim oPartComp As PartComponentDefinition
Dim oFeat As PartFeature
Dim oFeatProx As Object
For Each oOcc As ComponentOccurrence In oAsmComp.Occurrences
	If oOcc.Name = BrowserName Then
		PartExists = True
		oPattern.Add(oOcc)
	Else If oOcc.Name = RefPartName Then
	RefExists = True
	oPartComp = oOcc.Definition
	oFeat = oPartComp.Features.RectangularPatternFeatures.Item(FeatureName)
	oOcc.CreateGeometryProxy(oFeat, oFeatProx)
	End If
Next
If RefExists AndAlso PartExists
	'[ adds pattern into the assembly
	Dim oOccPtrns As OccurrencePatterns = oAsmComp.OccurrencePatterns
	Dim oPtrn As OccurrencePattern = oOccPtrns.AddFeatureBasedPattern(oPattern, oFeatProx)
	oPtrn.Name = PatternBrowserName
	']
End If
0 Likes
Message 3 of 3

felix.cortes5K3Y2
Advocate
Advocate

Thanks Joel, I wouldn't have been able to guess that!

 

Best,

Felix