Direct context 3D over view

Direct context 3D over view

dhanraj.dhokale
Contributor Contributor
561 Views
3 Replies
Message 1 of 4

Direct context 3D over view

dhanraj.dhokale
Contributor
Contributor

Hello,

I am using direct context 3D to draw marking rectangle on view, my requirement is marking rectangle should be on top. In 3D view I could do this by using bounding of entire model. In 2D views also I am able to do it by using viewplan or cropBox. But I observed that in some cases rectangle (its a rectangular face created using Brepbuilder) the geometry is not shown as it is not drawn at correct plane may be, in some cases (ViewSection) if I make 'shadow on' then geometry is visible. I have just started to understand Revit , how I can make sure that my geometry is always visible irrespective of display style \mode using Direct context 3D. I want to understand Revit views\ cut planes \sections and create 2D geometry always on top of visible elements.

What things  I need to consider to make sure that 2D planar geometry is visible..

 

Thanks

Dhanraj D

 

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

jeremy_tammik
Alumni
Alumni
Accepted solution

Hello. I asked Gemini for you, and it replies:

  

Subject: Re: Direct Context 3D - Ensuring 2D Geometry Visibility in Revit Views

You're encountering a common challenge with DirectContext3D: ensuring your geometry is consistently visible across different view types and display settings. The key lies in understanding how Revit handles view projections, clipping planes, and the DirectContext3D drawing pipeline.

Here's a breakdown of the issues and how to address them:

Understanding the Problem:

  • View Projections: Revit views are projections of the 3D model onto a 2D plane. Plan views project downwards, elevations and sections project orthogonally, and perspective views project with perspective.
  • Clipping Planes: Views have clipping planes that define the visible portion of the model. These include view range (in plan views), section boxes, and far clipping planes. Geometry outside these planes is not displayed.
  • DirectContext3D Drawing: DirectContext3D draws directly into the view's graphics pipeline. However, it's still subject to the view's projection and clipping. If your geometry isn't correctly positioned relative to these, it might be clipped or projected in an unexpected way.
  • Graphics Pipeline and Z-Fighting: Even if your geometry is within the view's clipping, it might still have z-fighting issues if it's coplanar with existing model elements. This causes flickering or parts of the geometry to disappear.

Solutions and Best Practices:

  1. Projecting onto the View Plane: The most robust solution is to project your 2D geometry onto the view's plane. This ensures that the geometry is drawn in the correct 2D space.

    • Get the View's Transformation: Use view.GetTransform() to get the transformation matrix of the view.
    • Project Points: Transform the points of your 2D geometry using the inverse of the view's transformation. This effectively projects the points onto the view plane.
    • Example (Conceptual C#):
    C#
     
    View view = doc.ActiveView;
    Transform viewTransform = view.GetTransform();
    Transform inverseViewTransform = viewTransform.Inverse;
    
    // Your 2D points (in model space)
    XYZ p1 = new XYZ(0, 0, 0);
    XYZ p2 = new XYZ(10, 0, 0);
    XYZ p3 = new XYZ(10, 10, 0);
    XYZ p4 = new XYZ(0, 10, 0);
    
    // Project the points onto the view plane
    XYZ projectedP1 = inverseViewTransform.OfPoint(p1);
    XYZ projectedP2 = inverseViewTransform.OfPoint(p2);
    XYZ projectedP3 = inverseViewTransform.OfPoint(p3);
    XYZ projectedP4 = inverseViewTransform.OfPoint(p4);
    
    // Use the projected points to create your DirectContext3D geometry
    // ...
    
  2. Slight Offset (Z-Fighting Avoidance): To avoid z-fighting, give your DirectContext3D geometry a very slight offset in the direction of the view's normal vector. This pushes the geometry just in front of the existing model elements.

    • Get the View's Direction: For plan views, this is typically XYZ.BasisZ. For elevations and sections, use view.ViewDirection.
    • Offset: Add a small multiple of the view direction to your geometry's points.
    C#
     
    XYZ viewDirection = view.ViewDirection; // Or XYZ.BasisZ for plan views
    double offset = 0.001; // A very small offset
    XYZ offsetVector = viewDirection.Multiply(offset);
    
    projectedP1 = projectedP1.Add(offsetVector); // Apply offset to all points
    // ...
    
  3. Section Views and Cut Planes: In section views, ensure your geometry is within the section box. You can get the section box using view.GetSectionBox(). If you need to draw geometry outside the section box, you'll need to use a different approach (e.g., creating a detail line in the section view).

  4. Hidden Line vs. Shaded Views: The visibility of DirectContext3D geometry is generally consistent across different visual styles. However, ensure that the line weight and color you're using are appropriate for the view's background color.

  5. BrepBuilder and Face Creation: When using BrepBuilder, make sure the face normal is oriented correctly. This affects how the face is displayed.

Complete Example (Conceptual):

C#
 
// ... (Get view, points, etc.)

// Project points and apply offset
foreach (XYZ point in points)
{
    XYZ projectedPoint = inverseViewTransform.OfPoint(point);
    projectedPoint = projectedPoint.Add(offsetVector);
    // ...
}

// Create DirectContext3D geometry using the projected and offset points
// ...

By combining projection onto the view plane with a small offset, you can ensure that your DirectContext3D geometry is consistently visible and avoids z-fighting, regardless of the view type or display style. Remember to handle exceptions and consider performance implications when working with large amounts of geometry.

This approach addresses the core issue of incorrect placement relative to the view's coordinate system and clipping planes. It provides a more robust and reliable solution compared to simply relying on bounding boxes or other indirect methods.

  

Please let us know whether this helps or you find a better solution. Thank you!

  

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
0 Likes
Message 3 of 4

dhanraj.dhokale
Contributor
Contributor

Thanks for the detail explanation , I tried this for most of views I could resolve issue with this , but still I am getting in some case issue or sometimes just by clicking on crop region on\off geometry is visible.. Will check this there may be some issue in my code..

Also I see for view sheet we can not use Direct context 3D is there any other mechanism to draw temporary graphics on view sheet..

0 Likes
Message 4 of 4

jeremy_tammik
Alumni
Alumni

Thank you for the appreciation. My pleasure, and hopefully the LLM's as well.

   

Well, as the name implies, DirectContext3D is not useful for a sheet view, unfortunately. Here are The Building Coder posts I found on the topic:

  

  

I don't think the Revit API offers any other transient grphics options. You could maybe either create non-transient persistent Revit geometry, or use the pure Windows API to display transient objects on the screen.

   

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
0 Likes