Updating the Wall Compound Layer Structure

We have a new Revit API supporter on the DevTech team!

Balaji joined ADN DevTech a year or two ago and dived straight into supporting AutoCAD with great zest, setting new records in speed and efficiency.
Now he addressed his first Revit API case, on how to update a wall compound layer structure.

We touched on this topic in the past when discussing the new

changes made to the CompoundStructure class
in
Revit 2012.

An example is also given by the CmdNewWallLayer external command in The Building Coder samples, which required a complete rewrite due to these changes when

migrating The Building Coder samples to Revit 2012
.

Here is Balaji’s case:

Question: I have been updating wall layers through the API, but I am having trouble with the layer thicknesses in a wall with a compound structure. What might I be doing wrong?

Answer: As explained in the ADN technical solution on

Changes to compound layer don’t apply to wall type
,
accessible to ADN members only, GetCompoundStructure works on a copy and the modifications to the compound structure do not change the original type until SetCompoundStructure is called.

Here is a snippet of sample code that uses the CompoundStructure SetLayerWidth method and works ok:


  UIApplication uiApp = commandData.Application;
  UIDocument uiDoc = uiApp.ActiveUIDocument;
  Document doc = uiDoc.Document;
 
  Transaction trans = new Transaction( doc );
  trans.Start( "changeLayerWidth" );
 
  Reference ref1 = uiDoc.Selection.PickObject(
    ObjectType.Element, "Pick a wall" );
 
  ElementId id = ref1.ElementId;
  Element elem = doc.get_Element( id );
  Wall wall = elem as Wall;
  if( wall == null )
    return Result.Failed;
 
  CompoundStructure cs
    = wall.WallType.GetCompoundStructure();
 
  double layerWidth = 0.2;
  int layerIndex = cs.GetFirstCoreLayerIndex();
 
  IList<CompoundStructureLayer> cslayers
    = cs.GetLayers();
 
  foreach( CompoundStructureLayer csl in cslayers )
  {
    cs.SetLayerWidth( layerIndex, layerWidth );
    layerIndex++;
  }
  wall.WallType.SetCompoundStructure( cs );
  trans.Commit();

I verified the total wall thickness and it does add up correctly.

Many thanks to Balaji for this solution, and congratulations on answering your first Revit API ADN case!
Welcome to the club!


Comments

11 responses to “Updating the Wall Compound Layer Structure”

  1. E.g. the main issue – we must don’t forget call SetCompoundStructure method if we want to save CompundStructure? API Help tell about it:)
    Hope Balaji will help me in future with my silly or not questions:)
    Best regards, Victor.

  2. Hello Jeremy,
    I’m trying to get the faces of a especific layer (and then get the Reference Object), during a CompoundStructure iteration. Is there any way I can achieve that?
    Thanks in advance

  3. Dear Victor,
    Yup, I guess that is about the gist of it.
    I must say, I have yet to hear a silly question from you.
    And anyway, some people say that silly questions do not exist at ll.
    Cheers, Jeremy.

  4. Dear Israel,
    If you want the reference to the face of a specific layer, then that layer and its face must be database resident.
    You could achieve this by splitting the wall or floor or whatever into parts, as described in
    http://thebuildingcoder.typepad.com/blog/2012/01/identifying-wall-compound-layers-and-parts.html
    and
    http://thebuildingcoder.typepad.com/blog/2011/10/retrieving-detailed-wall-layer-geometry.html
    The previous posts mentioned there discuss other ways of accessing wall layer geometry without splitting it into parts.
    Cheers, Jeremy.

  5. Dear Jeremy,
    I was joking about silly question.
    I try don’t to ask really stupid questions that sometimes happen and try to resolve it myself.
    And I’m really glad that you don’t think my questions are stupid.
    Best regards, Victor.

  6. Dear Victor,
    :-)
    Cheers, Jeremy.

  7. Hi, Jeremy
    in the sample code, what is the reason to set all the layers within the compoundlayers?
    foreach( CompoundStructureLayer csl in cslayers )
    {
    cs.SetLayerWidth( layerIndex, layerWidth );
    layerIndex++;
    }
    also, could you tell me what is the relationships for firstcorelayer, lastcorelayer and the rest of layers? how to find out the core structural layer thickness?
    thank a lot.
    Joe

  8. Dear Joe,
    I do not believe there is any special reason for that, it is just a test sample.
    Here are some little core related discussions:

    Core Structural Layer
    Setting the Compound Structure Core and Shell Layers

    Cheers, Jeremy.

  9. I tried the example to change the layer and I for an exception on the SetLayerWidth indicating that I…
    Cannot modify the width of an existing region which is not a simple region.
    Here is my code (SIPWT is a WallType I have found earlier:
    Using transaction As New Transaction(doc, “DuplicateWallType”)
    transaction.Start()
    Dim newWallType As WallType = TryCast(SIPWT.Duplicate(“SayWhat”), WallType)
    transaction.Commit()
    transaction.Dispose()
    End Using
    Using transaction As New Transaction(doc, “ChangeWallWidth”)
    transaction.Start()
    Dim FEC As New FilteredElementCollector(doc)
    FEC.OfClass(GetType(WallType))
    Dim newWallType As WallType = Nothing
    For Each WT As WallType In FEC
    If WT.Name = “SayWhat” Then
    newWallType = WT
    End If
    Next
    Dim cs As CompoundStructure = newWallType.GetCompoundStructure()
    Dim layerWidth As Double = 0.5
    Dim layerIndex As Integer = 0
    Dim cslayers As IList(Of CompoundStructureLayer) = cs.GetLayers()
    For Each csl As CompoundStructureLayer In cslayers
    cs.SetLayerWidth(layerIndex, layerWidth)
    layerIndex += 1
    Next
    newWallType.SetCompoundStructure(cs)
    transaction.Commit()
    transaction.Dispose()
    End Using
    The 2 transactions are a bit convoluted, but a result of me trying to find out what is going on…
    What would be the reason that I cant set the layerwidth to 0.5?
    Regards
    Drew Jarvis

  10. KellyR1641 Avatar
    KellyR1641

    I cannot get this to work either. When I set it up to tell me what the new thickness (width) is, it tells me it is what I have set, but when the transaction is committed, the layers remain unchanged.

Leave a Reply to Jeremy TammikCancel reply

Discover more from Autodesk Developer Blog

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

Continue reading