I created a node in C# to place BlockReferences in AutoCAD (I know there are OOTB nodes but there are reasons to use my own node).
This is the code:
using acDbServ = Autodesk.AutoCAD.DatabaseServices;
using dynACNodes = Autodesk.AutoCAD.DynamoNodes;
using dynAppServ = Autodesk.AutoCAD.DynamoApp.Services;
using Autodesk.DesignScript.Runtime;
using Autodesk.DesignScript.Geometry;
[Dynamo.Graph.Nodes.NodeCategory("Create")]
public static dynACNodes.Object CreateBlockReference(Point point, string blockname, string layer, [DefaultArgument("null")] List<string> attributes, [DefaultArgument("null")] Vector rotation)
{
dynACNodes.Object returnValue = null;
if (rotation == null) { rotation = Vector.ZAxis(); }
acDbServ.BlockReference block = Functions.PlaceBlock(blockname, point, Functions.CreateLayer(layer), attributes, rotation);
if (block != null)
{
returnValue = dynACNodes.Object.InternalMakeObject(block, true); // Newly created object should be owned by Dynamo.
}
return returnValue;
}
This works and the created BlockReference is acting on the Element Binding and the node returns a DynamoNodes.Object.
But I don’t want to return a DynamoNodes.Object, I want to return a DynamoNodes.BlockReference. But how?
A DynamoNodes.BlockReference is in fact a DynamoNodes.Object but I can’t return this:
dynACNodes.Object.InternalMakeObject(block, true) as dynACNodes.BlockReference;
Visual Studio accepts the casting but it result in null.
I’ve found something like this:
returnValue = dynAppServ.ObjectDescriptors.GetDescriptorByType("BlockReference").DynamoCreator(block, true) as dynACNodes.BlockReference;
This returns a DynamoNodes.BlockReference, and it is registered for Element Binding if I add True as parameter. Unfortunately there is no documentation about this function and the parameters so basically I have the feeling I am just trying and hoping for luck. It became probably too complex now.
So, the question: is there an easier way to return a DynamoNodes.Object as a DynamoNodes.BlockReference?