Create a view camera or 3d view with boundering

Create a view camera or 3d view with boundering

Anonymous
Not applicable
2,629 Views
3 Replies
Message 1 of 4

Create a view camera or 3d view with boundering

Anonymous
Not applicable

Hi! I am a new user in the api of revit

I am trying to create a image of a room to insert this in a sheet

At the beggining I star with boundering to obtain a 3dView but I didn't get a good perspective with this code:

                ViewFamilyType viewFamilyType = (from v in new FilteredElementCollector(doc).
                                                 OfClass(typeof(ViewFamilyType)).
                                                 Cast<ViewFamilyType>()
                                                 where v.ViewFamily == ViewFamily.ThreeDimensional
                                                 select v).First();

using (Transaction t3d = new Transaction(doc, "crop"))
                {
                    t3d.Start();
                    View3D view = View3D.CreateIsometric(doc, viewFamilyType.Id);
  view.HideElements(hidewallId);
                    view.HideElements(hidegridId);
                    //view.HideElements(hidesectionId);
                    view.HideElements(hidelevelId);
                   BoundingBoxXYZ box = room.get_BoundingBox(view);

                   XYZ maxbox = box.Max;
                   XYZ minbox = box.Min;

                   view.CropBox = box;

                    view.CropBoxActive = true;
                    //view.CropBoxVisible = true;


                    view.Scale = 50;
                   Viewport.Create(doc, sheet.Id, view.Id, a1p3);

                    t3d.Commit();
                }

The result is:

Captura.PNG

I can't get with this a view inside of the room... maybe it's not possible or i have to use the camera class, but I don't find how use it. Someone can help me. 

I want to get something like the next image

Captura2.PNG

Thanks you and best regards

Lorena 

0 Likes
Accepted solutions (1)
2,630 Views
3 Replies
Replies (3)
Message 2 of 4

aignatovich
Advisor
Advisor
Accepted solution

Hi!

Try this code:

public static View Create3DView(Room room)
{
	var document = room.Document;

	using (var transaction = new Transaction(document, "create 3d view"))
	{
		transaction.Start();

		var view = View3D.CreatePerspective(document, document.GetDefaultElementTypeId(ElementTypeGroup.ViewType3D));
		
		view.SetOrientation(GetOrientation(room));

		transaction.Commit();

		return view;
	}
}

private static ViewOrientation3D GetOrientation(Room room)
{
	var calculator = new SpatialElementGeometryCalculator(room.Document, new SpatialElementBoundaryOptions
		{
			SpatialElementBoundaryLocation = SpatialElementBoundaryLocation.Finish
		});

	// find first vertical boundary face
	var face = calculator
		.CalculateSpatialElementGeometry(room)
		.GetGeometry()
		.Faces
		.Cast<Face>()
		.First(x => Math.Abs(x.ComputeNormal(UV.Zero).DotProduct(XYZ.BasisZ)) < 1E-5);

	var faceBox = face.GetBoundingBox();

	var faceCenter = face.Evaluate(0.5*(faceBox.Min + faceBox.Max));

	return new ViewOrientation3D(faceCenter, XYZ.BasisZ, -1*face.ComputeNormal(UV.Zero));
}

Also note: there were some problems with 3d view creation on some releases of Revit 2016 and 2017

The result for the kitchen in the rac_basic_sample_project.rvt

view3d generation.PNG

 

Message 3 of 4

Anonymous
Not applicable

Thanks so much! this works very well!!!

0 Likes
Message 4 of 4

Anonymous
Not applicable

Hi! 

I have another doubt about this issue...

I changed a bit the view, placing the eye in the top of the face, and with a angle not perpendicular to the z-axis. But I need to have zoom out of the image or change the size of the box, and this is my problem, I don't know how could do it

I tried it, changing the boundingBox but this didn´t change…

My code is the next:

 

        private static ViewOrientation3D GetOrientation(Room room, Wall wall3DView, UIDocument uiDoc)
        {

            var calculator = new SpatialElementGeometryCalculator(room.Document, new SpatialElementBoundaryOptions
            {
                SpatialElementBoundaryLocation = SpatialElementBoundaryLocation.Finish
            });
            IList<Reference> sideFaces =
                HostObjectUtils.GetSideFaces(wall3DView,
                    ShellLayerType.Interior);
            Face face =
                uiDoc.Document.GetElement(sideFaces[0])
                    .GetGeometryObjectFromReference(sideFaces[0]) as Face;
            

            var faceBox = face.GetBoundingBox();

            var faceCenter = face.Evaluate(0.5 * (faceBox.Min + faceBox.Max));
           
            var dist = 0.5;
            var h = 0.15;
            
            XYZ heightFace = new XYZ(0, 0, h);
           
            var eye = faceCenter + heightFace;
            var angulo = -1 * face.ComputeNormal(UV.Zero) - heightFace / dist;
            var up = XYZ.BasisZ - 1 * face.ComputeNormal(UV.Zero) * h / dist;
         
            return new ViewOrientation3D(eye, up, angulo);
            
        }

In the next code, bounding is the bounding of the room...

public static View3D Create3DView(Room room, Wall wall3DView, UIDocument uiDoc,BoundingBoxXYZ bounding)
        {
            var document = room.Document;

            using (var transaction = new Transaction(document, "create 3d view"))
            {
                transaction.Start();

                var view = View3D.CreatePerspective(document, document.GetDefaultElementTypeId(ElementTypeGroup.ViewType3D));
                BoundingBoxXYZ bb = view.CropBox;

                
                
                bb.Max = bounding.Max;
                bb.Min = bounding.Min;

                view.CropBox = bb;
                view.SetOrientation(GetOrientation(room, wall3DView, uiDoc));
                transaction.Commit();

                return view;
            }
        }

 

Do you know where is the mistake? or do you know how could I make zoom out?

If I don´t use the bounding the solution will be:


 Capturakitchen.PNG


 And I want a solution like the next picture: 

 

 

Capturakitchen2.PNG

 

Thank you and best regards

 

Lorena

 

0 Likes