Attaching windows to walls

Alexey Abramovsky

July 06, 2017

In Teigha Architecture, objects can be attached to other objects. Such connections are handled by special anchor objects which are stored in a .dwg file just like any other object. An anchor is represented by the base class AECDbAnchor. The meaning of the connection between objects can be different: for example, windows are inserted in a wall, or a schedule table is attached to an object. Each type of connection is handled by a subclass of AECDbAnchor which implements connection semantics.
These connections are called “relations.” How can we see these relations in a drawing? For example, let’s look at a wall with windows:

image1

The wall is cut automatically in places where windows are inserted. This means that the wall knows about the openings attached to it. If we move the wall, the openings will move automatically:

image2

This also means that windows know that they are inserted in a wall. The relation “smth is attached to a wall” is handled by the AECDbAnchorEntToWall class and its subclasses.

How it works

In Teigha Architecture there is a special global object called a relation graph. It uses anchor objects to determine relationships between objects. When a wall is modified, the relation graph goes through the list of objects related to the wall and calls each object recalculation. Objects use their anchors to recalculate their new positions.

Example

How to attach a window to a wall?

void addWindowToWall(OdDbDatabasePtr pDb, OdDbObjectId idWall)
{
  double wW = 1200;
  double wH = 1300;

  //create a window of a certain window style. Window style should be already added to the database
  AECDbWindowPtr window1 = AECDbWindow::CreateAECObject(pDb->getModelSpaceId(), getWindowStyle(pDb));

  //set desired window size
  window1->SetWidth(wW);
  window1->SetHeight(wH);

  window1->SetOpenPercent(30);

  if ( !idWall.isNull() )
  {
    window1->AttachWallAnchor(idWall);

    //move window along the wall in xand z direction to a desired place
    AECDbAnchorEntToWallPtr anchor = window1->GetAnchor().openObject(OdDb::kForWrite);
    anchor->GetXParams()->SetOffset(400);
    anchor->GetZParams()->SetOffset(1000);
  }
}

The previous code is part of the AecHouseBuilder standard example. Refer to AECHouseBuilderObj::addWindows1stFloor() for full code.

Note: The AttachWallAnchor() function actually creates a new AECDbAnchorOpeningBaseToWall object, sets its reference to the wall ID, adds it to the database, and sets the window’s anchor reference to the created anchor.