void
ArxDbgUiTdcInsert::OnInsertBlkDef()
{
	CString blkName;
	m_ebDestName1.GetWindowText(blkName);

	if (!acdbSNValid(blkName, false)) {
		CString str;
		str.Format(_T("%s is not a valid block name."), blkName);
		ArxDbgUtils::alertBox(str);
		return;
	}

	UpdateData(TRUE);

	bool isExternal;
	AcDbDatabase* sourceDb = getSourceDb(isExternal);
	AcDbDatabase* destDb = getDestDb();
	ASSERT(sourceDb != destDb);

	Acad::ErrorStatus es;

		// lock the document we are inserting into
	ArxDbgDocLockWrite docLock(destDb);
	if (docLock.lockAndSetCurrent() != Acad::eOk)
		return;

	bool preserveSrcDb = intToBool(m_preserveOriginalDb);
	if (!preserveSrcDb && !isExternal) {
		ArxDbgUtils::alertBox(_T("Ignoring setting of \"Preserve source database\" since destroying\na database associated with a Document would be harsh."));
		preserveSrcDb = true;
	}

	AcDbObjectId blkId;
	es = destDb->insert(blkId, blkName, sourceDb, preserveSrcDb);
	if (es == Acad::eOk) {
		ArxDbgUiTdmDatabase dbox(destDb, this, _T("Inserted Database"));
		dbox.DoModal();

		doSaveOption(destDb);

		if (isExternal && !preserveSrcDb)
			reloadRaidedExternalDwg(sourceDb);
	}
	else {
		CString str;
		str.Format(_T("Insert failed: %s"), ArxDbgUtils::rxErrorStr(es));
		ArxDbgUtils::stopAlertBox(str);
	}
}
void
ArxDbgUiTdcWblockClone::fillBlockList() 
{
    m_blockList.RemoveAll();

    AcDbDatabase* curDb = getSourceDb();
    ASSERT(curDb != NULL);

    Acad::ErrorStatus es;
    es = ArxDbgUtils::collectBlockIds(m_blockList, false, false, false, curDb);

    ASSERT(es == Acad::eOk);

    ArxDbgUtils::fillListBox(m_blockList, m_puBlockDef, 0);
}
void
ArxDbgUiTdcWblockClone::setButtonModes()
{
    AcDbDatabase* sourceDb = getSourceDb();
    AcDbDatabase* destDb = getDestDb();

    bool allowClones;
    if (sourceDb == destDb)
        allowClones = false;
    else
        allowClones = true;

    ArxDbgUtils::enableDlgItem(this, ARXDBG_BN_WBLOCK_CLONE_SET, allowClones);
    ArxDbgUtils::enableDlgItem(this, ARXDBG_BN_WBLOCK_BLKDEF, allowClones);
    ArxDbgUtils::enableDlgItem(this, ARXDBG_BN_WBLOCK_ALL, allowClones);
}
void
ArxDbgUiTdcInsert::setButtonModes()
{
	bool isExternal;
	AcDbDatabase* sourceDb = getSourceDb(isExternal);
	AcDbDatabase* destDb = getDestDb();

	bool allowClones;
	if (sourceDb == destDb)
		allowClones = false;
	else
		allowClones = true;

    ArxDbgUtils::enableDlgItem(this, ARXDBG_BN_INSERT_ALL, allowClones);
    ArxDbgUtils::enableDlgItem(this, ARXDBG_BN_INSERT_AS_BLOCK, allowClones);
    ArxDbgUtils::enableDlgItem(this, ARXDBG_BN_TRANSFER_BLKDEF, allowClones);
}
void
ArxDbgUiTdcWblockClone::OnWblockBlkDef()
{
        // get the block we're suppose to be wblocking
    int index = m_puBlockDef.GetCurSel();
    ASSERT(index != CB_ERR);

    CString str;
    AcDbObjectId blkDefId;
    if (!m_blockList.GetAtIndex(index, str, blkDefId)) {
        ASSERT(0);
        return;
    }

    AcDbDatabase* sourceDb = getSourceDb();
    ASSERT(sourceDb != NULL);

        // lock the document we are wblocking from
    ArxDbgDocLockWrite docLock(sourceDb);
    if (docLock.lock() != Acad::eOk)
        return;

    // restore database to non-xref state (xes returns eOk if this
    // was necessary, and something else if it wasn't).
    Acad::ErrorStatus xes = sourceDb->restoreOriginalXrefSymbols();
    AcDbDatabase* tmpDb = NULL;
    Acad::ErrorStatus es = sourceDb->wblock(tmpDb, blkDefId);
    if (xes ==Acad::eOk)
        xes = sourceDb->restoreForwardingXrefSymbols();
    if (es == Acad::eOk) {
        doInsertOption(tmpDb);

        delete tmpDb;   // we're done with it
    }
    else {
        CString str;
        str.Format(_T("Wblock failed: %s"), ArxDbgUtils::rxErrorStr(es));
        ArxDbgUtils::stopAlertBox(str);

        delete tmpDb;   // yes, a failed wblock can leave a partially created database!
                        // so always delete it.
    }
}
void
ArxDbgUiTdcWblockClone::OnWblockObjects() 
{
    AcDbDatabase* sourceDb = getSourceDb();
    ASSERT(sourceDb != NULL);

    AcDbObjectIdArray objIds;
    m_cloneSet.getObjectsForDatabase(sourceDb, objIds);

    // unfortunately, wblock is a little restrictive on what
    // it will allow to be cloned automatically.  So, we factor
    // out all the entities and allow those to be cloned, and
    // then our ArxDbgAppEditorReactor will handle the rest by hand.
    AcDbObjectIdArray okToClone, mustCloneByHand;
    divideCloneSet(objIds, mustCloneByHand, okToClone);

    // lock the document we are wblocking from
    ArxDbgDocLockWrite docLock(sourceDb);
    if (docLock.lock() != Acad::eOk)
        return;
    // restore database to non-xref state (xes returns eOk if this
    // was necessary, and something else if it wasn't).
    Acad::ErrorStatus xes = sourceDb->restoreOriginalXrefSymbols();
    AcDbDatabase* tmpDb = NULL;
    Acad::ErrorStatus es = sourceDb->wblock(tmpDb, okToClone, m_basePt);
    if (xes ==Acad::eOk)
        xes = sourceDb->restoreForwardingXrefSymbols();
    if (es == Acad::eOk) {
        doInsertOption(tmpDb);

        delete tmpDb;   // we're done with it
    }
    else {
        CString str;
        str.Format(_T("Wblock failed: %s"), ArxDbgUtils::rxErrorStr(es));
        ArxDbgUtils::stopAlertBox(str);

        delete tmpDb;   // yes, a failed wblock can leave a partially created database!
                        // so always delete it.
    }
}
void
ArxDbgUiTdcInsert::OnInsertDb() 
{
	UpdateData(TRUE);

	bool isExternal;
	AcDbDatabase* sourceDb = getSourceDb(isExternal);
	AcDbDatabase* destDb = getDestDb();
	ASSERT(sourceDb != destDb);

	Acad::ErrorStatus es;

		// lock the document we are inserting into
	ArxDbgDocLockWrite docLock(destDb);
	if (docLock.lockAndSetCurrent() != Acad::eOk)
		return;

	bool preserveSrcDb = intToBool(m_preserveOriginalDb);
	if (!preserveSrcDb && !isExternal) {
		ArxDbgUtils::alertBox(_T("Ignoring setting of \"Preserve source database\" since destroying\na database associated with a Document would be harsh."));
		preserveSrcDb = true;
	}

	es = destDb->insert(AcGeMatrix3d::kIdentity, sourceDb, preserveSrcDb);
	if (es == Acad::eOk) {
		ArxDbgUiTdmDatabase dbox(destDb, this, _T("Inserted Database"));
		dbox.DoModal();

		doSaveOption(destDb);

		if (isExternal && !preserveSrcDb)
			reloadRaidedExternalDwg(sourceDb);
	}
	else {
		CString str;
		str.Format(_T("Insert failed: %s"), ArxDbgUtils::rxErrorStr(es));
		ArxDbgUtils::stopAlertBox(str);
	}
}
void
ArxDbgUiTdcWblockClone::OnWblockAll()
{
    UpdateData(TRUE);

    AcDbDatabase* sourceDb = getSourceDb();
    ASSERT(sourceDb != NULL);

    if (m_doForceCopy)
        sourceDb->forceWblockDatabaseCopy();

        // lock the document we are wblocking from
    ArxDbgDocLockWrite docLock(sourceDb);
    if (docLock.lock() != Acad::eOk)
        return;

    // restore database to non-xref state (xes returns eOk if this
    // was necessary, and something else if it wasn't).
    Acad::ErrorStatus xes = sourceDb->restoreOriginalXrefSymbols();
    AcDbDatabase* tmpDb = NULL;
    Acad::ErrorStatus es = sourceDb->wblock(tmpDb);
    if (xes ==Acad::eOk)
        xes = sourceDb->restoreForwardingXrefSymbols();
    if (es == Acad::eOk) {
        doInsertOption(tmpDb);

        delete tmpDb;   // we're done with it
    }
    else {
        CString str;
        str.Format(_T("Wblock failed: %s"), ArxDbgUtils::rxErrorStr(es));
        ArxDbgUtils::stopAlertBox(str);

        delete tmpDb;   // yes, a failed wblock can leave a partially created database!
                        // so always delete it.
    }
}
void
ArxDbgUiTdcInsert::OnTransferBlkDef()
{
		// get the block we're suppose to be wblocking
	int index = m_puBlockDef.GetCurSel();
	ASSERT(index != CB_ERR);

	CString srcBlkName;
	AcDbObjectId blkDefId;
	if (!m_blockList.GetAtIndex(index, srcBlkName, blkDefId)) {
		ASSERT(0);
		return;
	}

		// get name of destination block name
	CString destBlkName;
	m_ebDestName2.GetWindowText(destBlkName);

	if (!acdbSNValid(destBlkName, false)) {
		CString str;
		str.Format(_T("%s is not a valid block name."), destBlkName);
		ArxDbgUtils::alertBox(str);
		return;
	}

	UpdateData(TRUE);

	bool isExternal;
	AcDbDatabase* sourceDb = getSourceDb(isExternal);
	AcDbDatabase* destDb = getDestDb();
	ASSERT(sourceDb != destDb);

		// lock the document we are inserting into
	ArxDbgDocLockWrite docLock(destDb);
	if (docLock.lockAndSetCurrent() != Acad::eOk)
		return;

		// lock the document we are inserting from
	ArxDbgDocLockWrite docLock2(sourceDb);
	if (docLock2.lock() != Acad::eOk)
		return;

	bool preserveSrcDb = intToBool(m_preserveOriginalDb);
	if (!preserveSrcDb && !isExternal) {
		ArxDbgUtils::alertBox(_T("Ignoring setting of \"Preserve source database\" since destroying\na database associated with a Document would be harsh."));
		preserveSrcDb = true;
	}

	AcDbObjectId blkId;
	Acad::ErrorStatus es;
	es = destDb->insert(blkId, srcBlkName, destBlkName, sourceDb, preserveSrcDb);
	if (es == Acad::eOk) {
		ArxDbgUiTdmDatabase dbox(destDb, this, _T("Inserted Database"));
		dbox.DoModal();

		doSaveOption(destDb);

		if (isExternal && !preserveSrcDb)
			reloadRaidedExternalDwg(sourceDb);
	}
	else {
		CString str;
		str.Format(_T("Insert failed: %s"), ArxDbgUtils::rxErrorStr(es));
		ArxDbgUtils::stopAlertBox(str);
	}
}