Beam Maker Using a Void Extrusion to Cut

Here is an interesting and more than average advanced sample application
BeamMaker by
Marcelo Quevedo of

hsbSOFT
demonstrating
how to create a void in a beam in a family file.

The family file is created and loaded into the current working project on the fly.

The final result of the application is the following beam family instance created in the currently active document, which in this case was a new project:

Beam with a void extrusion cut out

Here is Marcelo’s description of his project:

Using the default template that comes with Revit, the sample creates a ‘structural framing’ family that contains a void extrusion that cuts the solid (existing extrusion in the Revit template).

  • The sample looks for the template, opens it, and creates a void extrusion in the position (0,0,0). It means that it is located in the center of the solid.
  • In order that the void extrusion can cut the solid, I am using the brilliant solution that Joe recommended me based on the CombineElements method.
  • After that, it loads the new family into an existing Revit project via the LoadFamily method.
  • Finally, using this new family, it creates a new instance in the existing project with the NewFamilyInstance method.

Here are some quick and dirty notes of my own from stepping through the application code in the debugger:

  • Check if the current document is a family document.
  • If not, a new family document based on the ‘Structural Framing – Beams and Braces’ family template file is created. The template file already contains one extrusion representing the beam element solid body.
  • Start a transaction.
  • Create a void extrusion by calling the NewExtrusion method with its first argument, bool isSolid, set to false to create a void. This requires the prior creation of the appropriate profile curve array array and sketch plane.
  • Collect all extrusions in the family document, e.g. the solid beam element and the void cut-out extrusion.
  • Apply the CombineElements method to them to cut the void part out of the solid part.
  • Load the newly created family into the current project.
  • Commit the transaction.
  • Insert a new family instance using the new family definition.

BeamMaker includes a whole bunch of useful utility functions, a real treasure trove of gems is contained in there.

As a very simple example, here is the GetFamilyTemplatePath which determines the full path name of a given family template file and makes use of the FamilyTemplatePath property on the Revit application object to do so.
It returns the full path of a default template for a given family template name:


private string GetFamilyTemplatePath(
  string strFamilyTemplateName )
{
  string strPathForTemplates
    = _app.FamilyTemplatePath;
 
  string strTemplateFile
    = Path.Combine( strPathForTemplates,
      strFamilyTemplateName );
 
  if( !File.Exists( strTemplateFile ) )
  {
    strTemplateFile = Path.Combine(
      strPathForTemplates,
      "Metric " + strFamilyTemplateName );
  }
 
  if( !File.Exists( strTemplateFile ) )
  {
    strTemplateFile = "";
  }
 
  return strTemplateFile;
}

Please download the
BeamMaker project including
the source code, Visual Studio solution and add-in manifest files to look at the complete code.

To wrap this up, here is the original discussion between Marcelo and Joe which prompted Marcelo to develop and provide this sample:

Question: Using the family API, I am creating a new structural framing family.
I am using the template that comes with Revit “Metric Structural Framing – Beams and Braces.rft”.
I am creating a void extrusion to cut the existing extrusion, but it does not cut the existing one.
If you manually edit the family, you can see the existing extrusion and the new void extrusion, but the void extrusion does not subtract the geometry from the other one.
How can I achieve this?

Answer: Simply creating the void solid does not automatically cause it to cut any existing ones.
In the Revit user interface, one needs to explicitly invoke the ‘Cut Geometry’ command for this to happen, i.e. to make the void cut the solid.
It is accessible through the Modify tab and Geometry panel.
In a Revit plug-in, one can use the CombineElements method to make the void objects cut the solid objects.

Many thanks to Joe and above all to Marcelo for providing this useful example and code!


Comments

13 responses to “Beam Maker Using a Void Extrusion to Cut”

  1. Jarico Avatar
    Jarico

    Hi,Jeremy,
    how can I do interference check through the API?
    since the Revit application provide a tool “Interference Check”,
    but what can we do it through the API?

  2. Dear Jarico,
    Unfortunately, there is no direct programmatic access to the built-in Revit Interference Check command.
    The Revit API does provide some lower level interference checking methods, which are described in the following post and the previous ones that it points to:
    http://thebuildingcoder.typepad.com/blog/2010/06/intersection-between-elements.html
    It would be possible to build a full interference checking tool using those methods. For an example of two such tools for specialised cases, please have a look at the Revit SDK samples AvoidObstruction and FindColumns.
    Cheers, Jeremy.

  3. nicholas.broadbent@hotmail.com Avatar
    nicholas.broadbent@hotmail.com

    Hi Jeremy,
    In a project i can get the File name of a family that has been loaded into the Project but not the Full Path of the family, How do i get the Full Path? Do i need to edit the family?

  4. Dear Nick,
    Good question. Unfortunately, I do not believe that there is any API access to the full path of the linked file:
    http://thebuildingcoder.typepad.com/blog/2008/12/linked-files.html
    Cheers, Jeremy.

  5. Hi Jeremy,
    Can i use this code for cutting the certain object? like room with boundaries?
    Thanks Sandeep

  6. Dear Sandeep,
    This can almost certainly not be used to cut a room and its boundaries, because that is not represented as a solid at all.
    Cheers, Jeremy.

  7. Hi Jeremy,
    In a project i am able to get FamilyTemplatePath. How can i get the selected template name?
    Please suggest.
    Thanks Bhavana

  8. Dear Bhavana,
    Well, there is of course a fundamental difference between the path and the selected filename in that the path is a pretty constant application-wide setting, whereas the selected filename is an ephemeral user selected choice of the moment.
    I don’t know off-hand. Some things you can try are to enumerate all the files in the current template path, and check the category of the current family document. That might at least help you limit down things. You could probably use Windows tools like FileMon or Process Monitor to determine which template file was read last by Revit, or the underlying Windows APIs to monitor the file access:
    http://technet.microsoft.com/en-us/sysinternals/bb896642
    http://technet.microsoft.com/en-us/sysinternals/bb896645
    What is the context within which you need this information, and what do you need it for?
    Cheers, Jeremy.

  9. Thank for this quick Reply.
    In my project
    i need to map families folder programmaticaly based on the selected template .
    Please suggest.
    Thanks
    Bhvaana

  10. Dear Bhavana,
    Why?
    What for?
    What does that mean?
    In what way is you mapping based on the selected template, and why does that have to be so?
    I don’t understand your objective at all, so there is nothing I can suggest.
    Cheers, Jeremy.

  11. Dear Jeremy,
    I have created some families with different family category
    when i want to load families in my project suupose i select electriccal template then only electrical families from a particular path needs to be loaded.
    Thanks
    Bhavana

  12. Dear Bhavana,
    Yes, that sounds sensible.
    Try checking the family category.
    Document.OwnerFamily.Category might help.
    Cheers, Jeremy.

  13. Hello Jeremy,
    I just downloaded the code, and run it without any change. but it always throw me an exception “Family Loading Failed”. Do you know why? Thanks

Leave a Reply to SandeepCancel reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading