How to import blocks from file

I want to implement a function like ARQ Style.
So first I use the command mmaBlockDef to generate a file include two blocks in its AllInstanceDefinitions property.
And then by command mmaBlockRestore I want to import blocks from the file and add them to current doc, but I don’t know how to implement, can you help me? thank you

My rhino is 7.2, and use rhino common, here is my code

namespace MMArc
{
  public class MMArcBlockDef : Command
  {
    public override string EnglishName => "mmaBlockDef";

    protected override Result RunCommand(RhinoDoc doc, RunMode mode)
    {
      var path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
      var filename = Path.Combine(path, "testBlocks.3dm");

      Result rc;

      using (var file = new File3dm())
      {
        var layer0 = new Layer { Name = "Default", Color = Color.Black };
        file.AllLayers.Add(layer0);
        var layer_index = file.AllLayers.Count - 1;
        var att = new ObjectAttributes { LayerIndex = layer_index };

        //first block
        {
          Point3d base_point = new Point3d(0, 0, 0);
          string idef_name = "mmBlock01";

          var geometry = new List<GeometryBase>();
          var attributes = new List<ObjectAttributes>();

          for (var x = 0; x < 10; x++)
          {
            var line_curve = new LineCurve(new Point3d(x, 0, 0), new Point3d(x, x * 10, 0));
            geometry.Add(line_curve);
            attributes.Add(att);
          }
          file.AllInstanceDefinitions.Add(idef_name, string.Empty, base_point, geometry, attributes);
        }

        //second block
        {
          Point3d base_point = new Point3d(0, 0, 0);
          string idef_name = "mmBlock02";

          var geometry = new List<GeometryBase>();
          var attributes = new List<ObjectAttributes>();

          for (var x = 0; x < 10; x++)
          {
            var line_curve = new LineCurve(new Point3d(x, 0, 0), new Point3d(x, x * 5, 0));
            geometry.Add(line_curve);
            attributes.Add(att);
          }
          file.AllInstanceDefinitions.Add(idef_name, string.Empty, base_point, geometry, attributes);
        }

        //write 3dm file
        rc = file.Write(filename, 7) ? Result.Success : Result.Failure;
      }

      return rc;
    }
  }

  /// <summary>
  /// read the .3dm file, and add its blocks into doc.InstanceDefinitions
  /// </summary>
  public class MMArcBlockRestore : Command
  {
    public override string EnglishName => "mmaBlockRestore";

    protected override Result RunCommand(RhinoDoc doc, RunMode mode)
    {
      var path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
      var filename = Path.Combine(path, "testBlocks.3dm");

      var file = new File3dm();
      file = File3dm.Read(filename);


      //??? How to add all InstantsDefinitions items in file to current doc ????
      /*for (int i=0; i< rfile.AllInstanceDefinitions.Count; i++)
      {
        var ins = file.AllInstanceDefinitions[i];
        doc.InstanceDefinitions.Add()
      }*/
      //???????

      Transform xform = Transform.Identity;
      doc.Objects.AddInstanceObject(0, xform);
      doc.Views.Redraw();
     

      return Result.Success;
    }
  }

}

Hi @chen_manhong,

Do you want to import an instance definition from a 3dm file, that may contain more than one instance definition? Or do you want to import the whole file as an instance definition, which could result in nested instances if the file contains instance definitions?

– Dale

1 Like

I’ve solved this problem using following code for the time being. I didn’t notice what you said. Maybe I’ll come back to study it later. Thank you dale.

Blockquote

var script = string.Format("_-Import “{0}” _Enter", filename);
RhinoApp.RunScript(script, true);

1 Like