void CDlgPgInfoAttachingPlacement::OnRemoveAttachingPlacement()
{
  ASSERT( m_iActivePlacement != -1);
  CModelerDoc* pDoc = theApp.GetDocument();
  ASSERT( pDoc != NULL);
  if( pDoc == NULL) return;
  CModelData *pMD = &pDoc->m_emEditModel.edm_md;

  pMD->md_aampAttachedPosition.Lock();
  pDoc->m_emEditModel.edm_aamAttachedModels.Lock();

  // get currently active placement from edit model
  CAttachedModel *pamAttachedModel =
    &pDoc->m_emEditModel.edm_aamAttachedModels[ m_iActivePlacement];
  // and from model data
  CAttachedModelPosition *pampModelPosition =
    &pMD->md_aampAttachedPosition[ m_iActivePlacement];

  pDoc->m_emEditModel.edm_aamAttachedModels.Unlock();
  pMD->md_aampAttachedPosition.Unlock();

  pDoc->ClearAttachments();

  pDoc->m_emEditModel.edm_aamAttachedModels.Delete( pamAttachedModel);
  pMD->md_aampAttachedPosition.Delete( pampModelPosition);

  pDoc->SetupAttachments();

  if( pDoc->m_emEditModel.edm_aamAttachedModels.Count() == 0)
  {
    m_iActivePlacement = -1;
  }
  if( m_iActivePlacement == pDoc->m_emEditModel.edm_aamAttachedModels.Count())
  {
    m_iActivePlacement = pDoc->m_emEditModel.edm_aamAttachedModels.Count()-1;
  }
  UpdateData(FALSE);
  pDoc->SetModifiedFlag();
  pDoc->UpdateAllViews( NULL);
}
void CDlgPgInfoAttachingPlacement::SetPlacementReferenceVertex(INDEX iCenter, INDEX iFront, INDEX iUp)
{
  // patch for calling before page is refreshed
  if(this == NULL) return;
  CModelerView *pModelerView = CModelerView::GetActiveView();
  if(pModelerView == NULL) return;
  CModelerDoc* pDoc = pModelerView->GetDocument();
  CModelData *pMD = &pDoc->m_emEditModel.edm_md;

  pMD->md_aampAttachedPosition.Lock();
  CAttachedModelPosition &amp = pMD->md_aampAttachedPosition[ m_iActivePlacement];

  // --------- Set axis-defining vertices, but swap if owerlaping
  // Center vertex
  if( iCenter != -1)
  {
    if(amp.amp_iFrontVertex == iCenter)
    {
      amp.amp_iFrontVertex = amp.amp_iCenterVertex;
    }
    else if(amp.amp_iUpVertex == iCenter)
    {
      amp.amp_iUpVertex = amp.amp_iCenterVertex;
    }
    amp.amp_iCenterVertex = iCenter;
  }
  // Front vertex
  if( iFront != -1)
  {
    if(amp.amp_iCenterVertex == iFront)
    {
      amp.amp_iCenterVertex = amp.amp_iFrontVertex;
    }
    else if(amp.amp_iUpVertex == iFront)
    {
      amp.amp_iUpVertex = amp.amp_iFrontVertex;
    }
    amp.amp_iFrontVertex = iFront;
  }
  // Up vertex  
  if( iUp != -1)
  {
    if(amp.amp_iCenterVertex == iUp)
    {
      amp.amp_iCenterVertex = amp.amp_iUpVertex;
    }
    else if(amp.amp_iFrontVertex == iUp)
    {
      amp.amp_iFrontVertex = amp.amp_iUpVertex;
    }
    amp.amp_iUpVertex = iUp;
  }

  pMD->md_aampAttachedPosition.Unlock();

  theApp.m_chGlobal.MarkChanged();
  pDoc->ClearAttachments();
  pDoc->SetupAttachments();
  pDoc->SetModifiedFlag();
  pDoc->UpdateAllViews( NULL);
}
void CDlgPgInfoAttachingPlacement::DoDataExchange(CDataExchange* pDX)
{
	CPropertyPage::DoDataExchange(pDX);

  CModelerView *pModelerView = CModelerView::GetActiveView();
  if(pModelerView == NULL) return;
  CModelerDoc* pDoc = pModelerView->GetDocument();
  CModelData *pMD = &pDoc->m_emEditModel.edm_md;

  INDEX ctPositions = pDoc->m_emEditModel.edm_aamAttachedModels.Count();
  if( (m_iActivePlacement == -1) && ( ctPositions != 0) ) m_iActivePlacement = 0;
  if( m_iActivePlacement >= ctPositions)
  {
    if( ctPositions != 0) m_iActivePlacement = 0;
    else                  m_iActivePlacement = -1;
  }

  // if transfering data from document to dialog
  if( !pDX->m_bSaveAndValidate)
  {
    
    BOOL bAttachmentExists = ( m_iActivePlacement != -1);
    GetDlgItem( IDC_ATTACHING_PLACEMENT_INDEX_T     )->EnableWindow( bAttachmentExists);
    GetDlgItem( IDC_PREVIOUS_ATTACHING_PLACEMENT    )->EnableWindow( bAttachmentExists);
    GetDlgItem( IDC_ATTACHING_PLACEMENT_NAME	      )->EnableWindow( bAttachmentExists);
    GetDlgItem( IDC_NEXT_ATTACHING_PLACEMENT	      )->EnableWindow( bAttachmentExists);
    GetDlgItem( IDC_REMOVE_ATTACHING_PLACEMENT      )->EnableWindow( bAttachmentExists);
    GetDlgItem( IDC_ATTACHING_PLACEMENT_X_OFFSET_T  )->EnableWindow( bAttachmentExists);
    GetDlgItem( IDC_ATTACHING_PLACEMENT_X_OFFSET    )->EnableWindow( bAttachmentExists);
    GetDlgItem( IDC_ATTACHING_PLACEMENT_Y_OFFSET_T  )->EnableWindow( bAttachmentExists);
    GetDlgItem( IDC_ATTACHING_PLACEMENT_Y_OFFSET    )->EnableWindow( bAttachmentExists);
    GetDlgItem( IDC_ATTACHING_PLACEMENT_Z_OFFSET_T  )->EnableWindow( bAttachmentExists);
    GetDlgItem( IDC_ATTACHING_PLACEMENT_Z_OFFSET    )->EnableWindow( bAttachmentExists);
    GetDlgItem( IDC_ATTACHING_PLACEMENT_HEADING_T   )->EnableWindow( bAttachmentExists);
    GetDlgItem( IDC_ATTACHING_PLACEMENT_HEADING     )->EnableWindow( bAttachmentExists);
    GetDlgItem( IDC_ATTACHING_PLACEMENT_PITCH_T     )->EnableWindow( bAttachmentExists);
    GetDlgItem( IDC_ATTACHING_PLACEMENT_PITCH       )->EnableWindow( bAttachmentExists);
    GetDlgItem( IDC_ATTACHING_PLACEMENT_BANKING_T   )->EnableWindow( bAttachmentExists);
    GetDlgItem( IDC_ATTACHING_PLACEMENT_BANKING     )->EnableWindow( bAttachmentExists);
    GetDlgItem( IDC_IS_VISIBLE		      )->EnableWindow( bAttachmentExists);
    GetDlgItem( IDC_BROWSE_MODEL		      )->EnableWindow( bAttachmentExists);
    GetDlgItem( IDC_MODEL_T			      )->EnableWindow( bAttachmentExists);
    GetDlgItem( IDC_ATTACHING_MODEL_T			      )->EnableWindow( bAttachmentExists);
    GetDlgItem( IDC_MODEL_ANIMATION_T 	      )->EnableWindow( bAttachmentExists);
    GetDlgItem( IDC_ATTACHMENT_MODEL_ANIMATION_COMBO)->EnableWindow( bAttachmentExists);
    GetDlgItem( IDC_VERTICES_T		      )->EnableWindow( bAttachmentExists);
    GetDlgItem( IDC_ATTACHING_VERTICES              )->EnableWindow( bAttachmentExists);
    
    if( bAttachmentExists)
    {
      pMD->md_aampAttachedPosition.Lock();
      pDoc->m_emEditModel.edm_aamAttachedModels.Lock();

      CPlacement3D plCurrent = pMD->md_aampAttachedPosition[ m_iActivePlacement].amp_plRelativePlacement;
	    m_fHeading	= DegAngle( plCurrent.pl_OrientationAngle(1));
	    m_fPitch	= DegAngle( plCurrent.pl_OrientationAngle(2));
      m_fBanking  = DegAngle( plCurrent.pl_OrientationAngle(3));
	    m_fXOffset	= plCurrent.pl_PositionVector(1);
	    m_fYOffset	= plCurrent.pl_PositionVector(2);
	    m_fZOffset	= plCurrent.pl_PositionVector(3);

      CAttachedModel *pam = &pDoc->m_emEditModel.edm_aamAttachedModels[ m_iActivePlacement];
      m_strName  = pam->am_strName;
      m_strAttachingModel = pam->am_moAttachedModel.GetName();
      char achrLine[ 256];
      sprintf( achrLine, "center:%d, front:%d, up:%d",
	pMD->md_aampAttachedPosition[ m_iActivePlacement].amp_iCenterVertex,
	pMD->md_aampAttachedPosition[ m_iActivePlacement].amp_iFrontVertex,
	pMD->md_aampAttachedPosition[ m_iActivePlacement].amp_iUpVertex);
      m_strAttachingVertices = achrLine;
      sprintf( achrLine, "%d.", m_iActivePlacement);
      m_strPlacementIndex = achrLine;

      m_bIsVisible =
	pDoc->m_emEditModel.edm_aamAttachedModels[ m_iActivePlacement].am_bVisible != 0;

      pDoc->m_emEditModel.edm_aamAttachedModels.Unlock();
      pMD->md_aampAttachedPosition.Unlock();

      if( IsWindow( m_comboAttachmentModelAnimation.m_hWnd))
      {
	m_comboAttachmentModelAnimation.EnableWindow( m_bIsVisible);
	FillAttachmentModelAnimationCombo();
      }

      // mark that the values have been updated to reflect the state of the view
      m_udAllValues.MarkUpdated();
    }
  }

	//{{AFX_DATA_MAP(CDlgPgInfoAttachingPlacement)
	DDX_Control(pDX, IDC_ATTACHMENT_MODEL_ANIMATION_COMBO, m_comboAttachmentModelAnimation);
	DDX_Text(pDX, IDC_ATTACHING_PLACEMENT_BANKING, m_fBanking);
	DDX_Text(pDX, IDC_ATTACHING_PLACEMENT_HEADING, m_fHeading);
	DDX_Text(pDX, IDC_ATTACHING_PLACEMENT_PITCH, m_fPitch);
	DDX_Text(pDX, IDC_ATTACHING_PLACEMENT_X_OFFSET, m_fXOffset);
	DDX_Text(pDX, IDC_ATTACHING_PLACEMENT_Y_OFFSET, m_fYOffset);
	DDX_Text(pDX, IDC_ATTACHING_PLACEMENT_Z_OFFSET, m_fZOffset);
	DDX_Text(pDX, IDC_ATTACHING_PLACEMENT_NAME, m_strName);
	DDX_Text(pDX, IDC_ATTACHING_MODEL_T, m_strAttachingModel);
	DDX_Text(pDX, IDC_ATTACHING_VERTICES, m_strAttachingVertices);
	DDX_Text(pDX, IDC_ATTACHING_PLACEMENT_INDEX_T, m_strPlacementIndex);
	DDX_Check(pDX, IDC_IS_VISIBLE, m_bIsVisible);
	//}}AFX_DATA_MAP
  // if transfering data from dialog to document

  if( pDX->m_bSaveAndValidate)
  {
    if( m_iActivePlacement == -1) return;
    pMD->md_aampAttachedPosition.Lock();
    pDoc->m_emEditModel.edm_aamAttachedModels.Lock();

    CPlacement3D plCurrent;
	  plCurrent.pl_OrientationAngle(1) = AngleDeg( m_fHeading);
	  plCurrent.pl_OrientationAngle(2) = AngleDeg( m_fPitch);
    plCurrent.pl_OrientationAngle(3) = AngleDeg( m_fBanking);
	  plCurrent.pl_PositionVector(1) = m_fXOffset;
	  plCurrent.pl_PositionVector(2) = m_fYOffset;
	  plCurrent.pl_PositionVector(3) = m_fZOffset;
    pMD->md_aampAttachedPosition[ m_iActivePlacement].amp_plRelativePlacement = plCurrent;
    pDoc->m_emEditModel.edm_aamAttachedModels[ m_iActivePlacement].am_strName = CStringA(m_strName);
    pDoc->m_emEditModel.edm_aamAttachedModels[ m_iActivePlacement].am_bVisible = m_bIsVisible;
    pDoc->m_emEditModel.edm_aamAttachedModels.Unlock();
    pMD->md_aampAttachedPosition.Unlock();

    pDoc->ClearAttachments();
    pDoc->SetupAttachments();

    pDoc->SetModifiedFlag();
    pDoc->UpdateAllViews( NULL);
  }
}