Create a Rectangular pattern of solids with iLogic

Create a Rectangular pattern of solids with iLogic

ejaL9K8H
Advocate Advocate
857 Views
12 Replies
Message 1 of 13

Create a Rectangular pattern of solids with iLogic

ejaL9K8H
Advocate
Advocate

Hi

 

I having problems achieving a rectangular pattern of solids with ilogic. This is my code so far.

' Create new pattern of solids

Dim oPD As PartDocument = ThisDoc.Document
Dim oPCD As PartComponentDefinition = oPD.ComponentDefinition
Dim Zaxis As Object = oPCD.WorkAxes.Item(3)' Select Z Axis

Dim SB_ObjCol As ObjectCollection
	SB_ObjCol = ThisApplication.TransientObjects.CreateObjectCollection
		For i = 1 To 5 ' Add the first 5 solid bodies to the object collection
			SurfBody = oPCD.SurfaceBodies.Item(i)
			SB_ObjCol.Add(SurfBody)
		Next

Dim qtypattern As Integer
qtypattern = 2 ' Define Number of Pattern
DistPrompt1 = 2000 ' Define Distance of Pattern

Dim oRecFeat As RectangularPatternFeature
oRecFeat = oPCD.Features.RectangularPatternFeatures.Add(SB_ObjCol, Zaxis, True, qtypattern, DistPrompt1, kDefault, , , , , , , , kAdjustToModelCompute, kAdjustToDirection1)

oRecFeat.PatternElements.PatternOfBody = True
oRecFeat.Definition.Operation = kNewBodyOperation

'rename pattern
oRecFeat.Name = "SolidPattern"

Any suggestions?

 

Best regards Emil Jakobsen

0 Likes
858 Views
12 Replies
Replies (12)
Message 2 of 13

ejaL9K8H
Advocate
Advocate

Alternatively a Rectangular Pattern Feature should be adjusted - But I cant get this to work either...

 

' Adjust solid pattern
Dim oPD As PartDocument = ThisDoc.Document
Dim oPCD As PartComponentDefinition = oPD.ComponentDefinition


Dim SB_ObjCol As ObjectCollection
	SB_ObjCol = ThisApplication.TransientObjects.CreateObjectCollection
		For i = 1 To 5 ' Add the first 5 solid bodies to the object collection
			SurfBody = oPCD.SurfaceBodies.Item(i)
			SB_ObjCol.Add(SurfBody)
		Next
		
Dim ComDef = ThisApplication.ActiveDocument.ComponentDefinition
Dim oPattern As RectangularPatternFeature = ComDef.Features.RectangularPatternFeatures.Item("SolidPattern")

'Trying to adjust the pattern
oPattern.SetAffectedBodies = SB_ObjCol

'SurfaceBodies is ReadOnly
'oPattern.SurfaceBodies = SB_ObjCol


 

0 Likes
Message 3 of 13

theo.bot
Collaborator
Collaborator

I'm not sure why you used the Add methode, because you can only use a Addbydefinition to create that pattern accoring to the API help.

 

theobot_0-1637748748648.png

 

When You first create a definition then you can add a collection of solids or a collection of features. here is a small sample:

 

Dim oDoc As PartDocument
oDoc = ThisDoc.Document 

Dim oComp As PartComponentDefinition
oComdef = oDoc.ComponentDefinition

Dim oSolids As ObjectCollection
	oSolids = ThisApplication.TransientObjects.CreateObjectCollection

oSolids.Add(oComdef.SurfaceBodies(1))
oSolids.Add(oComdef.SurfaceBodies(3))

Dim oRectangularPatterndef As RectangularPatternFeatureDefinition
oRectangularPatterndef = oComdef.Features.RectangularPatternFeatures.CreateDefinition(oSolids, oComdef.WorkAxes.Item("X Axis"), True, 3, 50)

oComdef.Features.RectangularPatternFeatures.AddByDefinition(oRectangularPatterndef)
0 Likes
Message 4 of 13

ejaL9K8H
Advocate
Advocate

I still get an error: 

Error in rule: Create new pattern, in document: Lamel.ipt

Unable to cast COM object of type 'System.__ComObject' to interface type 'Inventor.RectangularPatternFeature'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{58B0C13D-27CC-4F06-93FD-0524B69E6578}' failed due to the following error: En sådan grænseflade understøttes ikke (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).

 

With this code:

' Create new pattern of solids

Dim oPD As PartDocument = ThisDoc.Document
Dim oPCD As PartComponentDefinition = oPD.ComponentDefinition
Dim Zaxis As Object = oPCD.WorkAxes.Item(3)' Select Z Axis

Dim SB_ObjCol As ObjectCollection
	SB_ObjCol = ThisApplication.TransientObjects.CreateObjectCollection
		For i = 1 To 5 ' Add the first 5 solid bodies to the object collection
			SurfBody = oPCD.SurfaceBodies.Item(i)
			SB_ObjCol.Add(SurfBody)
		Next

Dim qtypattern As Integer
qtypattern = 2 ' Define Number of Pattern
DistPrompt1 = 2000 ' Define Distance of Pattern

Dim oRecFeat As RectangularPatternFeature
oRecFeat = oPCD.Features.RectangularPatternFeatures.CreateDefinition(SB_ObjCol, Zaxis, True, 3, 2000,kDefault)


oPCD.Features.RectangularPatternFeatures.AddByDefinition(oRecFeat)

'rename pattern
oRecFeat.Name = "SolidPattern"

 

0 Likes
Message 5 of 13

theo.bot
Collaborator
Collaborator
Can you share your model?
0 Likes
Message 6 of 13

ejaL9K8H
Advocate
Advocate

Does this make sense?

You can unsuppress the rectangular pattern with the name "What I want" to see what I am aiming for

0 Likes
Message 7 of 13

theo.bot
Collaborator
Collaborator

You where almost there 🙂

 

But you forgot to change the definition of oRecFeat to RectangularPatternFeatureDefinition

 

Also the renaming can't be done on the definition. do first you need to define the rectangularpatternfeature 

 

Also be aware you entered a value of 2000, meaning you will use the databaseunits ("cm"). Use the Unit of measure to work with different units. 

 

' Create new pattern of solids

Dim oPD As PartDocument = ThisDoc.Document
Dim oPCD As PartComponentDefinition = oPD.ComponentDefinition
Dim Zaxis As Object = oPCD.WorkAxes.Item(3)' Select Z Axis

Dim SB_ObjCol As ObjectCollection
	SB_ObjCol = ThisApplication.TransientObjects.CreateObjectCollection
		For i = 1 To 5 ' Add the first 5 solid bodies to the object collection
			SurfBody = oPCD.SurfaceBodies.Item(i)
			SB_ObjCol.Add(SurfBody)
		Next

Dim qtypattern As Integer
qtypattern = 2 ' Define Number of Pattern
DistPrompt1 = 2000 ' Define Distance of Pattern

'Dim oRecFeat As RectangularPatternFeature
Dim oRecFeat As RectangularPatternFeatureDefinition
oRecFeat = oPCD.Features.RectangularPatternFeatures.CreateDefinition(SB_ObjCol, Zaxis, True, 3, 2000,kDefault)

Dim oPattern As RectangularPatternFeature
oPattern = oPCD.Features.RectangularPatternFeatures.AddByDefinition(oRecFeat)

'rename pattern
oPattern.Name = "SolidPattern"

 

0 Likes
Message 8 of 13

ejaL9K8H
Advocate
Advocate

Thanks a lot!

But when i am copy ans pasting into a new rule, I keep getting an error:

 

System.Runtime.InteropServices.COMException (0x80004005): Uspecificeret fejl (Exception from HRESULT: 0x80004005 (E_FAIL))
at System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData)
at Inventor.RectangularPatternFeatures.AddByDefinition(RectangularPatternFeatureDefinition Definition)
at ThisRule.Main()
at Autodesk.iLogic.Exec.AppDomExec.ExecRuleInAssembly(Assembly assem)
at iLogic.RuleEvalContainer.ExecRuleEval(String execRule)

0 Likes
Message 9 of 13

ejaL9K8H
Advocate
Advocate

I think my problem is that I am having troublle with creating a ObjectColection with more than one solid.

 

This code with one solid works fine:

' Try To delete previous pattern, just For testing
Try
ThisApplication.ActiveDocument.ComponentDefinition.Features("Pattern").delete
InventorVb.DocumentUpdate()
Catch
End Try

Dim oPD As PartDocument = ThisApplication.ActiveDocument

Dim oPCD As PartComponentDefinition= oPD.ComponentDefinition
	
Dim oSolid1 As SurfaceBody = oPCD.SurfaceBodies(2)

Dim oBjCol As ObjectCollection= ThisApplication.TransientObjects.CreateObjectCollection
	oBjCol.Add(oSolid1)

Dim oPath As Object = oPCD.WorkAxes.Item(3)

Dim oRectDef As RectangularPatternFeatureDefinition =  oPCD.Features.RectangularPatternFeatures.CreateDefinition(oBjCol, oPath, True, 3, 12)
oRectDef.Operation = kNewBodyOperation

Dim oRecFeat As RectangularPatternFeature = oPCD.Features.RectangularPatternFeatures.AddByDefinition(oRectDef)

oRecFeat.Name = "Pattern"

But by adding 2 lines of code (2 solids) the code is giving an error:

' Try To delete previous pattern, just For testing
Try
ThisApplication.ActiveDocument.ComponentDefinition.Features("Pattern").delete
InventorVb.DocumentUpdate()
Catch
End Try

Dim oPD As PartDocument = ThisApplication.ActiveDocument

Dim oPCD As PartComponentDefinition= oPD.ComponentDefinition
	
Dim oSolid1 As SurfaceBody = oPCD.SurfaceBodies(2)
Dim oSolid2 As SurfaceBody = oPCD.SurfaceBodies(3) ' Adding this line

Dim oBjCol As ObjectCollection= ThisApplication.TransientObjects.CreateObjectCollection
	oBjCol.Add(oSolid1)
	oBjCol.Add(oSolid2) ' Adding this line

Dim oPath As Object = oPCD.WorkAxes.Item(3)

Dim oRectDef As RectangularPatternFeatureDefinition =  oPCD.Features.RectangularPatternFeatures.CreateDefinition(oBjCol, oPath, True, 3, 12)
oRectDef.Operation = kNewBodyOperation

Dim oRecFeat As RectangularPatternFeature = oPCD.Features.RectangularPatternFeatures.AddByDefinition(oRectDef)

oRecFeat.Name = "Pattern"

 

0 Likes
Message 10 of 13

theo.bot
Collaborator
Collaborator

Hmmm... strange..

I think it's a R2020 problem, because it works fine in R2021 and R2022. I can reproduce your error in R2020.

Currently update R202 to the latest update, see if this helps.

0 Likes
Message 11 of 13

theo.bot
Collaborator
Collaborator

I think I found a workaround for R2020.

We will add the solids after the definition creation.

 

' Try To delete previous pattern, just For testing
Try
ThisApplication.ActiveDocument.ComponentDefinition.Features("Pattern").delete
InventorVb.DocumentUpdate()
Catch
End Try

Dim oPD As PartDocument = ThisApplication.ActiveDocument

Dim oPCD As PartComponentDefinition= oPD.ComponentDefinition
	
Dim oSolid1 As SurfaceBody = oPCD.SurfaceBodies(2)

Dim oBjCol As ObjectCollection= ThisApplication.TransientObjects.CreateObjectCollection
	oBjCol.Add(oSolid1)

Dim oPath As Object = oPCD.WorkAxes.Item(3)

Dim oRectDef As RectangularPatternFeatureDefinition =  oPCD.Features.RectangularPatternFeatures.CreateDefinition(oBjCol, oPath, True, 3, 12)
oRectDef.Operation = kNewBodyOperation

For i = 2 To 5 ' Add the first 5 solid bodies to the object collection
oRectDef.ParentFeatures.Add(oPCD.SurfaceBodies.Item(i))
next

Dim oRecFeat As RectangularPatternFeature = oPCD.Features.RectangularPatternFeatures.AddByDefinition(oRectDef)

oRecFeat.Name = "Pattern"

 

0 Likes
Message 12 of 13

ejaL9K8H
Advocate
Advocate

It is still not working in R2020...

 

I think you need to add 

oRecFeat.PatternElements

But there is no add procedure... 

0 Likes
Message 13 of 13

theo.bot
Collaborator
Collaborator

Sorry, I was testing this to quickly between my other work. Never have three version of inventor open, that's asking for mistakes :-). Your right this also didn't work for R2020. I think you better start a support case, maybe it's a bug in the API of R2020.

0 Likes