void SurfaceConstructionVectorController::attachOtherModelObjects(const model::Surface& surface)
{
  boost::optional<model::Space> space = surface.space();
  if (space){
    attachOtherModelObjects(*space);
  }

  model::Building building = surface.model().getUniqueModelObject<model::Building>();
  attachOtherModelObjects(building);
}
void SubSurfaceConstructionVectorController::attachOtherModelObjects(const model::Building& building)
{
  attachOtherModelObject(building);

  boost::optional<model::SpaceType> spaceType = building.spaceType();
  if (spaceType){
    attachOtherModelObjects(*spaceType);
  }

  boost::optional<model::DefaultConstructionSet> defaultConstructionSet = building.defaultConstructionSet();
  if (defaultConstructionSet){
    attachOtherModelObjects(*defaultConstructionSet);
  }
}
void SpaceDesignSpecificationOutdoorAirVectorController::attach(const model::ModelObject& modelObject)
{
    ModelObjectVectorController::attach(modelObject);

    model::Space space = m_modelObject->cast<model::Space>();
    attachOtherModelObjects(space);
}
void SurfaceConstructionVectorController::attach(const model::ModelObject& modelObject)
{
  ModelObjectVectorController::attach(modelObject);

  model::Surface surface = m_modelObject->cast<model::Surface>();
  attachOtherModelObjects(surface);
}
void SubSurfaceConstructionVectorController::attachOtherModelObjects(const model::BuildingStory& buildingStory)
{
  attachOtherModelObject(buildingStory);

  boost::optional<model::DefaultConstructionSet> defaultConstructionSet = buildingStory.defaultConstructionSet();
  if (defaultConstructionSet){
    attachOtherModelObjects(*defaultConstructionSet);
  }
}
void SubSurfaceConstructionVectorController::attachOtherModelObjects(const model::SpaceType& spaceType)
{
  attachOtherModelObject(spaceType);

  boost::optional<model::DefaultConstructionSet> defaultConstructionSet = spaceType.defaultConstructionSet();
  if (defaultConstructionSet){
    attachOtherModelObjects(*defaultConstructionSet);
  }
}
void SubSurfaceConstructionVectorController::attachOtherModelObjects(const model::Space& space)
{
  attachOtherModelObject(space);

  boost::optional<model::DefaultConstructionSet> defaultConstructionSet = space.defaultConstructionSet();
  if (defaultConstructionSet){
    attachOtherModelObjects(*defaultConstructionSet);
  }

  boost::optional<model::SpaceType> spaceType = space.spaceType();
  if (spaceType && !space.isSpaceTypeDefaulted()){
    attachOtherModelObjects(*spaceType);
  }

  boost::optional<model::BuildingStory> buildingStory = space.buildingStory();
  if (buildingStory){
    attachOtherModelObjects(*buildingStory);
  }
}
void SubSurfaceConstructionVectorController::attachOtherModelObjects(const model::DefaultConstructionSet& defaultConstructionSet)
{
  attachOtherModelObject(defaultConstructionSet);

  boost::optional<model::DefaultSubSurfaceConstructions> defaultSubSurfaceConstructions = defaultConstructionSet.defaultExteriorSubSurfaceConstructions();
  if (defaultSubSurfaceConstructions){
    attachOtherModelObjects(*defaultSubSurfaceConstructions);
  }

  defaultSubSurfaceConstructions = defaultConstructionSet.defaultInteriorSubSurfaceConstructions();
  if (defaultSubSurfaceConstructions){
    attachOtherModelObject(*defaultSubSurfaceConstructions);
  }
}
void SpaceDesignSpecificationOutdoorAirVectorController::onChangeRelationship(const model::ModelObject& modelObject, int index, Handle newHandle, Handle oldHandle)
{
    if (modelObject.optionalCast<model::Space>()) {
        if (index == OS_SpaceFields::DesignSpecificationOutdoorAirObjectName) {
            emit itemIds(makeVector());
        } else if (index == OS_SpaceFields::SpaceTypeName) {
            detachOtherModelObjects();
            model::Space space = m_modelObject->cast<model::Space>();
            attachOtherModelObjects(space);
            emit itemIds(makeVector());
        }
    } else if (modelObject.optionalCast<model::Building>()) {
        if (index == OS_BuildingFields::SpaceTypeName) {
            detachOtherModelObjects();
            model::Space space = m_modelObject->cast<model::Space>();
            attachOtherModelObjects(space);
            emit itemIds(makeVector());
        }
    } else if (modelObject.optionalCast<model::SpaceType>()) {
        if (index == OS_SpaceTypeFields::DesignSpecificationOutdoorAirObjectName) {
            emit itemIds(makeVector());
        }
    }
}
void SubSurfaceConstructionVectorController::onChangeRelationship(const model::ModelObject& modelObject, int index, Handle newHandle, Handle oldHandle)
{
  model::SubSurface subSurface = m_modelObject->cast<model::SubSurface>();

  if (modelObject.optionalCast<model::SubSurface>()){
    if (index == OS_SubSurfaceFields::ConstructionName){
      emit itemIds(makeVector());
    }else if (index == OS_SubSurfaceFields::SurfaceName){
      detachOtherModelObjects();
      attachOtherModelObjects(subSurface);
      emit itemIds(makeVector());
    }
  }else if (modelObject.optionalCast<model::Surface>()){
    if (index == OS_SurfaceFields::SpaceName){
      detachOtherModelObjects();
      attachOtherModelObjects(subSurface);
      emit itemIds(makeVector());
    }
  }else if (modelObject.optionalCast<model::Space>()){
    if (index == OS_SpaceFields::SpaceTypeName){
      detachOtherModelObjects();
      attachOtherModelObjects(subSurface);
      emit itemIds(makeVector());
    }else if (index == OS_SpaceFields::BuildingStoryName){
      detachOtherModelObjects();
      attachOtherModelObjects(subSurface);
      emit itemIds(makeVector());
    }else if (index == OS_SpaceFields::DefaultConstructionSetName){
      detachOtherModelObjects();
      attachOtherModelObjects(subSurface);
      emit itemIds(makeVector());
    }
  }else if (modelObject.optionalCast<model::SpaceType>()){
    if (index == OS_SpaceTypeFields::DefaultConstructionSetName){
      detachOtherModelObjects();
      attachOtherModelObjects(subSurface);
      emit itemIds(makeVector());
    }
  }else if (modelObject.optionalCast<model::Building>()){
    if (index == OS_BuildingFields::DefaultConstructionSetName){
      detachOtherModelObjects();
      attachOtherModelObjects(subSurface);
      emit itemIds(makeVector());
    }else if (index == OS_BuildingFields::SpaceTypeName){
      detachOtherModelObjects();
      attachOtherModelObjects(subSurface);
      emit itemIds(makeVector());
    }
  }else if (modelObject.optionalCast<model::BuildingStory>()){
    if (index == OS_BuildingStoryFields::DefaultConstructionSetName){
      detachOtherModelObjects();
      attachOtherModelObjects(subSurface);
      emit itemIds(makeVector());
    }
  }else if (modelObject.optionalCast<model::DefaultConstructionSet>()){
    if (index == OS_DefaultConstructionSetFields::DefaultExteriorSubSurfaceConstructionsName){
      detachOtherModelObjects();
      attachOtherModelObjects(subSurface);
      emit itemIds(makeVector());
    }else if (index == OS_DefaultConstructionSetFields::DefaultInteriorSubSurfaceConstructionsName){
      detachOtherModelObjects();
      attachOtherModelObjects(subSurface);
      emit itemIds(makeVector());
    }
  }else if (modelObject.optionalCast<model::DefaultSubSurfaceConstructions>()){
    emit itemIds(makeVector());
  }
}