Adding a Body Modifier to a Wall

By Mikako Harada

Issue

Is there a sample code to add a body modifier to a wall? 

Solution

Below is a sample command to add a body modifier to a wall.  To test this, draw a wall and a mass element intersecting each other. Then, pick a wall and a mass element.

_
  Public Sub AddWallBodyModifier()
 
    ' Get the necessary helper objects up front 
    Dim db As Database =
      HostApplicationServices.WorkingDatabase
    Dim tm As TransactionManager = db.TransactionManager
    Dim tr As Transaction = tm.StartTransaction()
    Dim ed As Editor =
      Application.DocumentManager.MdiActiveDocument.Editor
 
    ' (1) Select a wall 
    
    Dim selOptWall As New PromptEntityOptions(
      "Select a wall to add a modifier")
    selOptWall.SetRejectMessage("Not a Wall")
    selOptWall.AddAllowedClass(GetType(Wall), True)
    Dim selResWall As PromptEntityResult = ed.GetEntity(selOptWall)
    If selResWall.Status  PromptStatus.OK Then
      Exit Sub
    End If
 
    ' (2) Select a mass element 
    
    Dim selOptMass As New PromptEntityOptions(
      "Select a mass element to add as a modifier")
    selOptMass.SetRejectMessage("Not a Mass Element")
    selOptMass.AddAllowedClass(GetType(MassElement), True)
    Dim selResMass As PromptEntityResult = ed.GetEntity(selOptMass)
    If selResMass.Status  PromptStatus.OK Then
      Exit Sub
    End If
 
    Try
      ' If we come to this point, we got a wall object. open it. 
      Dim obj As AcObject =
        tr.GetObject(selResWall.ObjectId, AcDb2.OpenMode.ForWrite)
      Dim pWall As Wall = CType(obj, Wall)
 
      ' And mass element. open it. 
      Dim objMass As AcObject =
        tr.GetObject(selResMass.ObjectId, AcDb2.OpenMode.ForRead)
      Dim pMass As MassElement = CType(objMass, MassElement)
 
      ' Get the wall style 
      Dim objStyle As AcObject =
        tr.GetObject(pWall.StyleId, AcDb2.OpenMode.ForRead)
      Dim pWallStyle As WallStyle = CType(objStyle, WallStyle)
 
      ' (3) Get the component id. 
      ' For simplicity, we are hard coding to add the body to the 
      ' first wall component. If you would like to choose a specific 
      ' component, look for a specific component in the wall style.
 
      Dim idComp As Short = pWallStyle.Components(0).ComponentId()
 
      ' Get the inverse of ecs. we'll need this to set the right 
      ' location. 
 
      Dim wcsToEcs As Matrix3d = pWall.Ecs.Inverse()
 
      ' (4) Here is the main part to add a body modifier to the wall. 
       
      ' Create a custom geometry body from the mass element. 
      
      Dim pBody As WallCustomGeometryBody =
        New WallCustomGeometryBody
      pBody.SetToStandard(db)
      pBody.SetBodyFromMassElement(pMass, wcsToEcs)
      pBody.Description = "body modifier added by ACA .NET"
      pBody.OperationType =
        WallCustomGeometryOperationType.AdditiveCutOpenings
      pBody.ComponentId = idComp
 
      ' Finally add the body to the wall as a custom geometry 
      ' component.
 
      pWall.WallCustomGeometry.Add(pBody)
 
      tr.Commit()
 
    Catch ex As Exception
      ed.WriteMessage(
        "Error in AddWallBodyModifier: " + ex.Message + vbLf)
      tr.Abort()
    Finally
      tr.Dispose()
    End Try
 
  End Sub

Comments

2 responses to “Adding a Body Modifier to a Wall”

  1. I have used code like this with success in the past for adding a body modifier to a wall. I have tried the same technique for adding geometry to a duct fitting after it has been placed in the model with absolutely no success. Is there a way using a similiar technique to add geometry to catalog based parts in Autocad MEP? The content builder does not allow for certain scenarios and being able to add the geometry at run time would help fix this drawback with the Content Builder.

  2. That looks like a scary mess of code to me, but Im a noob at coding. Glad you guys know what the heck you are doing. Im more of an end user CAD guy.

Leave a Reply

Discover more from Autodesk Developer Blog

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

Continue reading