Example code for a rectangular pattern of parts (occurrences?) in an assembly?

Example code for a rectangular pattern of parts (occurrences?) in an assembly?

oransen
Collaborator Collaborator
960 Views
4 Replies
Message 1 of 5

Example code for a rectangular pattern of parts (occurrences?) in an assembly?

oransen
Collaborator
Collaborator

Hello All,

 

Is there any example code for a rectangular pattern of parts (occurrences?) in an assembly?

 

I've looked around but can't find one.

 

Imagine I have a Part in an Assembly, how to I programatically create an array of these parts?

 

TIA

0 Likes
Accepted solutions (2)
961 Views
4 Replies
Replies (4)
Message 2 of 5

chandra.shekar.g
Autodesk Support
Autodesk Support
Accepted solution

Hi @oransen,

 

Try the following sample VBA code with attached assembly file.

 

Sub Main()

    Dim oDoc As AssemblyDocument
    Set oDoc = ThisApplication.ActiveDocument
    
    Dim oDef As AssemblyComponentDefinition
    Set oDef = oDoc.ComponentDefinition
    
    Dim objColl As ObjectCollection
    Set objColl = ThisApplication.TransientObjects.CreateObjectCollection
    
    Call objColl.Add(oDef.Occurrences.Item(2))
    
    Dim oPattern As RectangularOccurrencePattern
    Set oPattern = oDef.OccurrencePatterns.AddRectangularPattern(objColl, oDef.WorkAxes.Item(3), False, "10 mm", 3)
    
End Sub

 

Before Pattern.JPGAfter Pattern.JPG

 

Please feel free to contact if there is any doubt.

 

If solves problem, click on "Accept as solution" / give a "Kudo".

 

Thanks and regards,


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



Message 3 of 5

oransen
Collaborator
Collaborator
Accepted solution

While you were doing that in VB, for which many thanks, I wrote this in C++:

 

 

bool CreateYArrayOfParts (CComPtr<ComponentOccurrences> pOccurrencesList, // The occurrences in the assembly
                          CComPtr<AssemblyComponentDefinition> pCctsAssemblyCompDef,   // def of the assembly
                          CComPtr<ComponentOccurrence> pObjOcc,
                          const double kDeltaYMm,
                          const UINT ikCount) 
{
    CComPtr<OccurrencePatterns> pOccPatterns ;
    pOccPatterns = pCctsAssemblyCompDef->GetOccurrencePatterns();
    if (pOccPatterns == nullptr) {
        gLogger.Printf(ekErrMsg, "GetOccurrencePatterns failed");
        return false;
    }

    CComPtr<TransientObjects> pTransientObjects = theApp.GetTransientObjectsPtr() ; 

    CComVariant varObjEnumerator;
    CComPtr<ObjectCollection> pObjectCollection;
    HRESULT hRes = pTransientObjects->CreateObjectCollection(varObjEnumerator, &pObjectCollection);
    if (FAILED(hRes)) {
        gLogger.Printf(ekErrMsg, "CreateObjectCollection failed");
        return false;
    }

    // Add the part which is the basis to the pattern...
    hRes = pObjectCollection->Add (pObjOcc) ;
    if (FAILED(hRes)) {
        gLogger.Printf(ekErrMsg, "Add to ObjectCollection failed");
        return false;
    }

    // Get hold of the y axis...
    CComPtr<WorkAxis> pYWorkAxis ;
    GetAsmWorkAxisByName (pYWorkAxis,L"Y Axis",pCctsAssemblyCompDef) ;
    if (FAILED(hRes)) {
        gLogger.Printf(ekErrMsg, "Could not get assembly y axis");
        return false;
    }

    pOccPatterns->AddRectangularPattern(pObjectCollection, 
                                        _variant_t((IDispatch *)pYWorkAxis),
                                        VARIANT_FALSE,
                                        CComVariant(kDeltaYMm/10.0), //ColumnOffset As Variant, 
                                        CComVariant(ikCount), // ColumnCount As Variant, 
                                        CComVariant(), // [RowEntity] As Variant, 
                                        VARIANT_TRUE, // [RowEntityNaturalDirection] As Boolean, 
                                        CComVariant(0.0),// [RowOffset] As Variant, 
                                        CComVariant(1)); //  [RowCount] As Variant)

    return (hRes == S_OK) ;

...so our readers willl have two examples!  🙂

0 Likes
Message 4 of 5

niranjan934
Contributor
Contributor

I need to pattern an existing component pattern which you shown above image 2 . I need to pattern image 2 with visual basic

0 Likes
Message 5 of 5

oransen
Collaborator
Collaborator

Ah, sorry, I don't know how to do that, I've never needed it.  😞

0 Likes