Block RhinoCommon is not working

CreateBlock RhinoCommon is not working.
Please see the link below : It does not create any block.

public static Rhino.Commands.Result CreateBlock(Rhino.RhinoDoc doc)
{
// Select objects to define block
var go = new Rhino.Input.Custom.GetObject();
go.SetCommandPrompt( “Select objects to define block” );
go.ReferenceObjectSelect = false;
go.SubObjectSelect = false;
go.GroupSelect = true;

// Phantoms, grips, lights, etc., cannot be in blocks.
var forbidden_geometry_filter = Rhino.DocObjects.ObjectType.Light |
Rhino.DocObjects.ObjectType.Grip | Rhino.DocObjects.ObjectType.Phantom;
var geometry_filter = forbidden_geometry_filter ^ Rhino.DocObjects.ObjectType.AnyObject;
go.GeometryFilter = geometry_filter;
go.GetMultiple(1, 0);
if (go.CommandResult() != Rhino.Commands.Result.Success)
return go.CommandResult();

// Block base point
Rhino.Geometry.Point3d base_point;
var rc = Rhino.Input.RhinoGet.GetPoint(“Block base point”, false, out base_point);
if (rc != Rhino.Commands.Result.Success)
return rc;

// Block definition name
string idef_name = “”;
rc = Rhino.Input.RhinoGet.GetString(“Block definition name”, false, ref idef_name);
if (rc != Rhino.Commands.Result.Success)
return rc;
// Validate block name
idef_name = idef_name.Trim();
if (string.IsNullOrEmpty(idef_name))
return Rhino.Commands.Result.Nothing;

// See if block name already exists
Rhino.DocObjects.InstanceDefinition existing_idef = doc.InstanceDefinitions.Find(idef_name, true);
if (existing_idef != null)
{
Rhino.RhinoApp.WriteLine(“Block definition {0} already exists”, idef_name);
return Rhino.Commands.Result.Nothing;
}

// Gather all of the selected objects
var geometry = new System.Collections.Generic.List<Rhino.Geometry.GeometryBase>();
var attributes = new System.Collections.Generic.List<Rhino.DocObjects.ObjectAttributes>();
for (int i = 0; i < go.ObjectCount; i++)
{
var rhinoObject = go.Object(i).Object();
if (rhinoObject != null)
{
geometry.Add(rhinoObject.Geometry);
attributes.Add(rhinoObject.Attributes);
}
}

// Gather all of the selected objects
int idef_index = doc.InstanceDefinitions.Add(idef_name, string.Empty, base_point, geometry, attributes);

if( idef_index < 0 )
{
Rhino.RhinoApp.WriteLine(“Unable to create block definition”, idef_name);
return Rhino.Commands.Result.Failure;
}

return Rhino.Commands.Result.Success;
}

I just ran the sample code and it seem to work fine, as expected.

Note, this code create a block definition - it does not insert a block reference. Use the Insert command to do this.

Does this help?

Oh ! OK. Yes it is working fine with insert command…Thanks Dale.
Can I write a code which will insert a block reference?
I need to write a code.
Would you please add a bit of code for inserting the same block ?

If I want to insert the same block at the Point3D(0.0,0.0,0.0)

Or any sample code for inserting block would be a big help.
Many Thanks !
T

1 Like

This should do it:

protected override Result RunCommand(RhinoDoc doc, RunMode mode)
{
  var idef = doc.InstanceDefinitions.Find("Test", true);
  if (null != idef)
  {
    var xform = new Transform(1.0);
    doc.Objects.AddInstanceObject(idef.Index, xform);
    doc.Views.Redraw();
  }
  return Result.Success;
}
2 Likes

Thanks Dale !
I deleted a code here… Actually I need to insert the block on a specific point.

For example the given point is (233.03, 110.70, 0.0)

Please write me something for that… I found some sample code regarding transforming object to a picked point and there were override methods.

Please let me know a simplest way how to insert my block to this given point
(233.03, 110.70, 0.0)

… I am looking for a RhinoCommon c# version.

2 Likes