DirectShape with GeometryInstance and non conformal Transform

thomas.hain
Explorer

DirectShape with GeometryInstance and non conformal Transform

thomas.hain
Explorer
Explorer

This question relates to this one

 

https://forums.autodesk.com/t5/revit-api-forum/access-transform-of-a-directshape/m-p/13100704#M82239

 

I've created a DirectShape with GeometryInstances using the DirectShapeLibrary.

I've realized it is possible to create GeometryInstance with a transform that has non uniform scale. This is nice, since I want to visualize geometry of a custom tool with meshes and arbitrary transform matrices.

 

Unfortunatly the DirectShape can't be selected in 3DView only in PlanView.

 

Has anyone experienced this before?

 

Here some example code.  If you don't scale BasisX everything is fine.

 

 

 

[Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
class CmdTest : IExternalCommand
{
    public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
    {
        var doc = commandData.Application.ActiveUIDocument.Document;

        var p0 = new XYZ(-1, -1, 0);
        var p1 = new XYZ(1, -1, 0);
        var p2 = new XYZ(1, 1, 0);
        var p3 = new XYZ(-1, 1, 0);
        var line0 = Line.CreateBound(p0, p1);
        var line1 = Line.CreateBound(p1, p2);
        var line2 = Line.CreateBound(p2, p3);
        var line3 = Line.CreateBound(p3, p0);
        var loop = new CurveLoop();
        loop.Append(line0);
        loop.Append(line1);
        loop.Append(line2);
        loop.Append(line3);
        var loops = new CurveLoop[] { loop };
        var solid = GeometryCreationUtilities.CreateExtrusionGeometry(loops, XYZ.BasisZ, 1.0);

        using (var transaction = new Transaction(doc, "Create DS Box"))
        {
            transaction.Start();

            var dsType = DirectShapeType.Create(doc, "Box", new ElementId(BuiltInCategory.OST_GenericModel));
            dsType.SetShape([solid]);

            var dsLib = DirectShapeLibrary.GetDirectShapeLibrary(doc);
            dsLib.AddDefinitionType("Box", dsType.Id);

            var transform = new Transform(Transform.Identity);

            transform.BasisX *= 2; // stretch the box, non conformal

            var directShape = DirectShape.CreateElement(doc, new ElementId(BuiltInCategory.OST_GenericModel));
            var geoInst = DirectShape.CreateGeometryInstance(doc, "Box", transform);
            directShape.SetShape(geoInst);
            directShape.SetTypeId(dsType.Id);

            transaction.Commit();
        }

        return Result.Succeeded;
    }
}

 

 

If

0 Likes
Reply
150 Views
2 Replies
Replies (2)

ctm_mka
Advocate
Advocate

i have no experiencing using the DirectShapeLibrary, so i'm not entirely sure what your "Box" is. But try modifying your setshape (line 49) like so:

directshape.SetShape(new GeometryObject[] {geoInst});

 

0 Likes

thomas.hain
Explorer
Explorer

Thanks. But this doesn't work. The variable geoInst is already an IList<GeometryObject>.

 

Above code with a "Box" is only a simplified example. Real structure is more complex.

"Box" is just an identifier to refer to a registered geometry or DirectShapeType within the Library.

DirectShapeLibrary enables me to crate DirectShapes with Instances referencing the Geometry of a DirectShapeType.

So I have many Instances with different Transforms for one Geometry. It is the same structure my external data format has. The Geometry Instance has a Transform property which an AddIn can read back.

 

This works fine. But Instances with non conformal Transform (e.g. non uniform scale) are not selectable in the UI.

 

Cheers, Tom

 

0 Likes