.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How to create AssocArray properly?

3 REPLIES 3
SOLVED
Reply
Message 1 of 4
shricharana_bharadwaj
194 Views, 3 Replies

How to create AssocArray properly?

Hi,

I'm trying to create an assocarray in C# from a selected block. The problem is the array created will always be at the origin of the drawing not the position of the selected block. The End goal is to use this array in a point selection Jig. Please keep that in mind.

I've added code to transform each item back to its supposed to be position. But the controls of the array still remain at the origin of the drawing. 

How can I fix this?

 

Example of the current situation: (the position of the blocks itself is correct but the handles of the assocarray are at the base.)

shricharana_bharadwaj_0-1734522476419.png

 

The following is the code I've written.

 

using (Transaction tr = Active.Document.TransactionManager.StartUndoGroupTransaction())
{
    try
    {
        PromptEntityOptions pro = new PromptEntityOptions("Select block");
        pro.SetRejectMessage("Select block only");
        pro.AddAllowedClass(typeof(BlockReference), true);
        pro.AllowNone = false;

        PromptEntityResult per = Active.Editor.GetEntity(pro);

        if (per.Status != PromptStatus.OK)
        {
            Active.WriteCancel();
            return;
        }

        ObjectId obj = per.ObjectId;
        
        BlockReference br = (BlockReference)tr.GetObject(obj, OpenMode.ForWrite);



        AssocArrayRectangularParameters param = new AssocArrayRectangularParameters(100, 100, 0, 10, 10, 0, 0, 0);
        VertexRef verref = new VertexRef(br.Position);
        AssocArray asrray = AssocArray.CreateArray(new ObjectIdCollection { br.ObjectId }, verref, param);
        Matrix3d transform = Matrix3d.Displacement(br.Position - Point3d.Origin);
        AssocArrayRectangularParameters par = (AssocArrayRectangularParameters)asrray.GetParameters();

        

        AssocManager.EvaluateTopLevelNetwork(Active.Database, null, 0);

        ItemLocator[] items = asrray.getItems(true);

        foreach (ItemLocator item in items)
        {
            asrray.TransformItemBy(item, transform);
        }

        br.Erase();
        //Aisle.DeleteRefBlocks(TR);

        tr.Commit();
    }
    catch (System.Exception ex)
    {
        Active.WriteError(ex.Message);
        tr.Abort();
    }
    
}

 

 

Labels (3)
3 REPLIES 3
Message 2 of 4

Hi,

 

You can simply set the position of the newly created block reference (or direcly jig the block reference).

 

[CommandMethod("CREATEASSOCARRAY")]
public static void CreateAssociatveArray()
{
    var doc = Application.DocumentManager.MdiActiveDocument;
    var db = doc.Database;
    var ed = doc.Editor;

    var peo = new PromptEntityOptions("\nSelect block reference: ");
    peo.SetRejectMessage("\nSelected object is not a block reference.");
    peo.AddAllowedClass(typeof(BlockReference), true);
    var per = ed.GetEntity(peo);
    if (per.Status != PromptStatus.OK) return;

    using (var tr = db.TransactionManager.StartTransaction())
    {
        var source = (BlockReference)tr.GetObject(per.ObjectId, OpenMode.ForWrite);
        var parameters = new AssocArrayRectangularParameters(100, 100, 0, 10, 10, 0, 0, 0);
        var vertexRef = new VertexRef(source.Position);
        var assocArray = AssocArray.CreateArray(new ObjectIdCollection { source.ObjectId }, vertexRef, parameters);
        var assocArrayBlock = (BlockReference)tr.GetObject(assocArray.EntityId, OpenMode.ForWrite);
        assocArrayBlock.Position = source.Position;
        source.Erase();
        tr.Commit();
    }
}

 

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 3 of 4

Hi, 

 

I did not realize AssocArray can be used as a block reference. I'll try this.

 

Thank you

 

Message 4 of 4

Hi, 

Thank you. 

Your suggestion worked. I'm using transformby instead of directly setting the position, as I will be dealing with attributed blocks and I'm not sure setting the position directly would ignore the attributes as is the case for setting position of blocks directly.

 

 

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

AutoCAD Inside the Factory


Autodesk Design & Make Report