How to rotate elements in view3D in REVIT using API

How to rotate elements in view3D in REVIT using API

Anonymous
Not applicable
1,261 Views
6 Replies
Message 1 of 7

How to rotate elements in view3D in REVIT using API

Anonymous
Not applicable

Hi, 

 

Here what I want to do : I want to create a view3D and rotate elements on the view before exporting the view

 

I have the following code :

```

...

var viewFamilyType
= new FilteredElementCollector(doc)
.OfClass(typeof(ViewFamilyType))
.OfType<ViewFamilyType>()
.FirstOrDefault(x =>
x.ViewFamily == ViewFamily.ThreeDimensional);

using (var t = new Transaction(doc))
{
t.Start("Create 3D View");

if (viewFamilyType != null)
{
var threeDView = View3D.CreateIsometric(
doc, viewFamilyType.Id);

var o = new XYZ(0, 0, 0);
var x = new XYZ(1, 0, 0);


// Find all Wall instances in the document by using category filter

RotateModel(doc, threeDView, o, x, Math.PI / 2);

// Export to SAT

var viewSet = new List<ElementId>()
{
threeDView.Id
};
var exportOptions = new SATExportOptions();
doc.Export(outputFolder, file, viewSet, exportOptions);
}

t.RollBack();
}

...

 

private static void RotateModel(Document doc, View3D view, XYZ origin, XYZ axis, double angle)
{
view.Pinned = false;

// Use ElementClassFilter to find all loads in the document
// Using typeof(Element) will yield all AreaLoad, LineLoad and PointLoad
var filter = new ElementClassFilter(typeof(Extrusion));


// Use ElementClassFilter to find all loads in the document
// Using typeof(LoadBase) will yield all AreaLoad, LineLoad and PointLoad
var ox = Line.CreateBound(origin, axis);

var collector = new FilteredElementCollector(doc, view.Id);
if (!collector.IsValidObject)
throw new Exception("Not valid Object");

// Apply the filter to the elements in the active document
var elements = collector
.WhereElementIsNotElementType();
//.WherePasses(filter).ToList();

//var elements = collector.ToList();

foreach (var element1 in elements)
{
var element = (Extrusion) element1;
if (element.IsSolid && element.IsValidObject && !element.IsHidden(view))
ElementTransformUtils.RotateElement(doc, element.Id, ox, angle);
}


view.Pinned = true;

}

```

 

I get a "c'ant rotate element." I not sure I have the right methodology. Can someone help me?

 

Regards, 

0 Likes
1,262 Views
6 Replies
Replies (6)
Message 2 of 7

naveen.kumar.t
Autodesk Support
Autodesk Support

Hi @Anonymous ,

Change line from

var x = new XYZ(1, 0, 0);

to

 var x = new XYZ(0, 0, 1);

 I think this will work.

 

Also, check elements in var elements = collector.WhereElementIsNotElementType() is not NULL.

 

I hope this helps.


Naveen Kumar T
Developer Technical Services
Autodesk Developer Network

0 Likes
Message 3 of 7

Anonymous
Not applicable

Thanks for our help, I have tested your solution but I still get the same message.

I am just wondering how rotate work in REVIT, is it possible to apply rotation on any axis?

0 Likes
Message 4 of 7

naveen.kumar.t
Autodesk Support
Autodesk Support

Hi @Anonymous ,

ElementTransformUtils.RotateElements rotates an element about the given axis and angle.

Here is the code I used

var viewFamilyType = new FilteredElementCollector(doc).OfClass(typeof(ViewFamilyType)).OfType<ViewFamilyType>().FirstOrDefault(x => x.ViewFamily == ViewFamily.ThreeDimensional);
            using (var t = new Transaction(doc))
            {
                t.Start("Create 3D View");
                if (viewFamilyType != null)
                {
                    var threeDView = View3D.CreateIsometric(doc, viewFamilyType.Id);
                    var o = new XYZ(0, 0, 0);
                    var x = new XYZ(0, 0, 1);
                    // Find all Wall instances in the document by using category filter
                    RotateModel(doc, threeDView, o, x, Math.PI / 2);
                    // Export to SAT
                    var viewSet = new List<ElementId>()
                          {
                        threeDView.Id
                          };
                    var exportOptions = new SATExportOptions();
                    doc.Export(outputFolder,"Name.sat", viewSet, exportOptions);
                }
                t.Commit();
            }

In the below code use appropriate filter to get the element from the family instances

private static void RotateModel(Document doc, View3D view, XYZ origin, XYZ axis, double angle)
        {
            view.Pinned = false;
            var filter = new ElementClassFilter(typeof(FamilyInstance)); 
            var ox = Line.CreateBound(origin, axis);
            var collector = new FilteredElementCollector(doc, view.Id);
            if (!collector.IsValidObject)
                throw new Exception("Not valid Object");
            var elements = collector.WherePasses(filter).WhereElementIsNotElementType();
            foreach (var element1 in elements)
            {
                var element = (FamilyInstance)element1;
                if (element.IsValidObject && !element.IsHidden(view))
                    ElementTransformUtils.RotateElement(doc, element.Id, ox, angle);
            }
            view.Pinned = true;
        }

Also, refer the attached images(I just created a simple extrusion)😅

BEFOREBEFOREBEFORE

/////////////////////////////////////////////////////////////////////////////////////////////////////

AFTER.pngAFTER


Naveen Kumar T
Developer Technical Services
Autodesk Developer Network

0 Likes
Message 5 of 7

TripleM-Dev.net
Advisor
Advisor

Hi,

 

You can't rotate walls around the X-axis, rotation around the Z-axis could fail if there are hosted elements.

 

Further I see you want to create a SAT file, any change to export to Solidworks?

As I gather, solidworks has a the Y-axis as Up and not the Z-axis.

 

Could be corrected in SolidWorks or else just export to DWG and link-rotate in AutoCAD and create SAT from there?

I have consultants working in SolidWorks and they can get it correctly imported AND exported.

 

- Michel

0 Likes
Message 6 of 7

Anonymous
Not applicable

Thanks, true, it is to import into SolidWorks. I'll try to rotate the model so that it is correctly oriented in SolidWorks.

Is you said, it can't be oriented around ? What is the reason?

0 Likes
Message 7 of 7

TripleM-Dev.net
Advisor
Advisor

Have you tried it in the UI, if you can rotate the wall around the X-axis.

If something can't be done in the UI, it can't be done in the API.

 

Walls are levelbased, export the 3D view to AutoCAD, rotate there and then export to SAT from AutoCAD.

Or adjust the import in Solidworks

0 Likes