How to import selected blocks from a File3dm? (Or were blocks saved incorrectly in this file?)

rhinoformac
rhinocommon
rhino5

#1

Aim

I want to programatically filter which blocks to import from a given file. As a particular example where it does not work see leg_caster.3dm (4.1 MB)
.

I was unsuccessful because File3dmObjects I can add via doc.InstanceDefinitionAdd() but in this file I cannot find them based on their block definition name scanning File3dm.Objects. InstanceDefinitionGeometry however, I can find with File3dm.InstanceDefinitions.Where() but doc.InstanceDefinitionAdd() called with the fund objects returns always -1.

Attempts

(1) In all of my files so far but one it works by filtering by the names of the objects via the RhinoCommon API because the object names correspond to the block definition names: File3dm.Objects.Select(x=>x.Name == "myName).
Yet, when I read the attached file in my RhinoCommons plugin with File3dm.Objects.toArray() I find the object names to be empty or “Rhinoceros Binary STL (Aug 29 106)” although the blocks are named “swivel caster”, “chair leg” etc.

(2) However, when importing the file with the _-Import macro the block names gets added to my open file. But with this script macro I cannot selectively import blocks because the BlockManager is not yet scriptable on OS X (see this forum entry: How to import/export block from/to external file into open document?)

(3) I also tried to find the right instance definitions based on their name via File.InstanceDefinitions.

var blocks = file.InstanceDefinitions.Where((arg) ⇒ arg.Name == "caster"); // gives Rhino.Geometry.InstanceDefinitionGeometry, how can I get the Instance?
var geometries = new List<GeometryBase> {blocks.First()};
var definitionID = doc.InstanceDefinitions.Add(blocks.First().Name, "Description",
                                               Point3d.Origin,
                                               geometries);  // returns -1
doc.Objects.AddInstanceObject(definitionID, Tranform.Identity);

Is there a defect in the Rhino file API when writing this file? How can I repair my file? Or is there a better API to access the blocks and add them to my current document?

Setup

Xamarin Studio 6.1.2, .NET Mono 4.5
Rhinoceros for Mac 5.2.3


How to import/export block from/to external file into open document?
(Dale Fugier) #2

Hi Willi,

Does this example help?

https://github.com/dalefugier/SampleCsCommands/blob/master/SampleCsImportBlockFromFile.cs

– Dale


#3

Great, thank you very much!
Now, one question remains. How can I add instances of the added InstanceDefinitions that have the same transformation as in the original file? This happens automatically when I import the entire external file with

RhinoApp.RunScript("_-Import " + FileName + " _Enter", true);

However, when I add the instances like in your code example and then insert an instance Object via

doc.Objects.AddInstanceObject(definitionID, Transform.Identity)

obviously, the object is not transformed as it was in the source document. I know that for this purpose I would have to get the InstanceObject.InstanceXform. But how can I find the InstanceObject in the source file?