Inventor: Create Pattern of Component Occurrences

by Vladimir Ananyev

Issue

How to create a pattern (rectangular/circular) of occurrences in an Inventor assembly?

Solution

The sample code below shows how you could pattern occurrences in the x and y direction. The value no_x_rect  refers to the number of occurrences in the x direction, no_y_rect refers to the number of occurrences in  the y direction.  This example just creates a pattern along the x and y directions, you could modify this code to create patterns along any desired directions (e.g. you may pass in an EdgeProxy to define the directions). Similarly, you could also create circular patterns except that you would be dealing with angular co-ordinates.

 

VBA sample code:

Sub CreatePatternsInAssembly()

 

  ‘open an assembly with at least two components and run this sample

 

  Dim oAssyDoc As AssemblyDocument

  Set oAssyDoc = ThisApplication.ActiveDocument

 

  Dim oAssyDef As AssemblyComponentDefinition

  Set oAssyDef = oAssyDoc.ComponentDefinition

 

  If oAssyDef.Occurrences.Count < 2 Then

    MsgBox "Assembly must have 2 components"

    Exit Sub

  End If

 

  ‘base work axes

  Dim XAxis As WorkAxis

  Dim YAxis As WorkAxis

  Dim Zaxis As WorkAxis

  With oAssyDef

    Set XAxis = .WorkAxes(1)

    Set YAxis = .WorkAxes(2)

    Set Zaxis = .WorkAxes(3)

  End With

 

  ‘Create an object collection

  Dim objCol As ObjectCollection

  Set objCol = ThisApplication _

    .TransientObjects.CreateObjectCollection

 

  ‘add the desired occurrence to be patterned

  objCol.Add oAssyDef.Occurrences.Item(1)

 

  ‘set the number of patterns in the x direction

  Dim no_x_rect As Integer

  no_x_rect = 3

 

  ‘set the number of patterns in the y direction

  Dim no_y_rect As Integer

  no_y_rect = 3

  ‘Creating a Rectangular pattern

  Call oAssyDef.OccurrencePatterns _

        .AddRectangularPattern(objCol, _

          XAxis, True, 10, no_x_rect, _

          YAxis, True, 10, no_y_rect)

 

 

  ‘Creating a Circular pattern

  objCol.Clear

  objCol.Add oAssyDef.Occurrences.Item(1)

  Call oAssyDef.OccurrencePatterns _

        .AddCircularPattern(objCol, _

          Zaxis, True, "45 deg", 5)

 

End Sub

 

 

C# sample code:

 


private void CreatePatternsInAssembly()

{ //open an assembly with at least two components and run this sample

   

    AssemblyDocument oAssyDoc = (AssemblyDocument)oApp.ActiveDocument; 

    AssemblyComponentDefinition oAssyDef = oAssyDoc.ComponentDefinition;

 

  if (oAssyDef.Occurrences.Count < 2)

  {

    MessageBox.Show("Assembly must have 2 components");

    return;

  }

 

    //base work axes

    WorkAxis XAxis = oAssyDef.WorkAxes[1];

    WorkAxis YAxis = oAssyDef.WorkAxes[2];

    WorkAxis ZAxis = oAssyDef.WorkAxes[3];

 

    //Create an
object collection

    ObjectCollection objCol = oApp

        .TransientObjects.CreateObjectCollection();

 

    //add the desired occurrence to be patterned

    objCol.Add(oAssyDef.Occurrences[1]);

 

    //set the number of patterns in the x direction

    int no_x_rect = 3; 

    //set the number of patterns in the y direction

    int no_y_rect = 3;

 

    //Creating a Rectangular pattern

    oAssyDef.OccurrencePatterns.AddRectangularPattern(

        objCol, XAxis, true, 10, no_x_rect,

                YAxis, true, 10, no_y_rect); 

 

    //Creating a Circular pattern

    objCol.Clear();

    objCol.Add(oAssyDef.Occurrences[1]);

    oAssyDef.OccurrencePatterns.AddCircularPattern(

        objCol, ZAxis, true, "45 deg", 5);

<

p style=”line-height: normal;margin: 0cm 0cm 0pt” class=”MsoNormal”>}


Comments

3 responses to “Inventor: Create Pattern of Component Occurrences”

  1. Xuegeng12 Avatar
    Xuegeng12

    Thanks for your explain. But when I paste these VBA code into the VBA editor of Inventor, the former “AddRectangularPattern” method can excute, but the latter “AddCircularPattern”method CANNOT excute, which means that the “Add***Pattern” can excute only once!! What’s the reason of that? Thanks !

  2. como se puede programar para piezas no para ensambles y en c#, si me puedes ayudar, gracias

  3. Best thing is to ask on the Inventor Customization forum: https://forums.autodesk.com/t5/inventor-customization/bd-p/120

Leave a Reply to Xuegeng12Cancel reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading