C# Copy Blocks

Hello,
I’m trying to do the sequence in C#:
Select Block in rhino and copy it to another point.
And all does not workong propely for Bocks. What am I doing wrong?


protected override Result RunCommand(RhinoDoc doc, RunMode mode)
{
//01 - Point variable
var pt0 = new Point3d(0, 0, 0);
var pt1 = new Point3d(10, 0, 0);

        //02 - Selecting Block
        var go = new GetObject();
        go.SetCommandPrompt("Select Block");
        go.GeometryFilter = ObjectType.InstanceReference;
        go.SubObjectSelect = false;
        go.Get();

        if (go.CommandResult() != Result.Success)
            return go.CommandResult();

        //03 - Get the selected object
        var PBlockBeam = go.Object(0).Object() as InstanceObject;
        if (PBlockBeam == null)
            return Result.Failure;
           
        //04 - Vector
        var MoveObject = new Vector3d (pt1 - pt0);

        //05 - Copy the block to the destination point
        var PBlockBeamCopy = PBlockBeam.DuplicateGeometry();
        PBlockBeamCopy.Translate(MoveObject);

        //06 - Add the copy to the document
        doc.Objects.Add(PBlockBeamCopy);
        doc.Views.Redraw();
        return Result.Success;

    }

Hi @WM_BLOK_Mateusz_Gesl,

Here is a sample you might want to review.

SampleCsMove.cs

– Dale

Hello Dale,
No, unfortunately that’s not it. Your code is for moving.
I still can’t build a C# code to copy an element that is a block. I can do code for copying different elements, but copying a block from one point to another doesn’t work and I don’t know why.

Hi @WM_BLOK_Mateusz_Gesl ,
Unfortunately as of now, ObjectTable.Add() does not support adding InstanceReferenceGeometry objects. You can use the ObjectTable.AddInstanceObject( Int32, Transform) method instead

//01 - Point variable
var pt0 = new Point3d(0, 0, 0);
var pt1 = new Point3d(10, 0, 0);

        //02 - Selecting Block
        var go = new GetObject();
        go.SetCommandPrompt("Select Block");
        go.GeometryFilter = ObjectType.InstanceReference;
        go.SubObjectSelect = false;
        go.Get();

        if (go.CommandResult() != Result.Success)
            return go.CommandResult();

        //03 - Get the selected object
        var PBlockBeam = go.Object(0).Object() as InstanceObject;
        if (PBlockBeam == null)
            return Result.Failure;

        //04 - Vector
        var MoveObject = new Vector3d(pt1 - pt0);

      
        //06 - Create new Transform by multiplying the Translation, and the already existing transformation of the selected block
        Transform transform = Transform.Translation(MoveObject) * PBlockBeam.InstanceXform;

        //06 - Add new Instance object at the new Location
        doc.Objects.AddInstanceObject(PBlockBeam.InstanceDefinition.Index, transform);

        doc.Views.Redraw();
        return Result.Success;

Thank you Darryl. Now Everything working excellent!

Continuing the discussion from C# Copy Blocks:

I have one more question regarding the above C# code.
Is it possible to save the new copied BLOCK as a variable.
I ask because I would then like to extend the CODE and rotate and scale the copied BLOCK?