void CMyDlg::UpdateListCtr() { acDocManager->lockDocument(curDoc()); int nCount = m_ListCtr.GetItemCount(); acutPrintf("%d\n", nCount); long lId; CString str; AcDbObjectId LineId; AcDbEntity *pEnt = NULL; AcDbLine *pLine = NULL; Acad::ErrorStatus es; if (nCount > 0) { for (int i = 0; i < nCount; ++i) { str = m_ListCtr.GetItemText(i, 0); lId = atol(str); LineId.setFromOldId(lId); //获得指针 es = acdbOpenAcDbEntity(pEnt, LineId, AcDb::kForWrite); //检查是否被删除 pLine = AcDbLine::cast(pEnt); if (es == Acad::eWasErased) { m_ListCtr.DeleteItem(i); --m_lLineCnt; --i; --m_Row; } //检查颜色 else if (es == Acad::eOk) { if (pLine) { str = m_ListCtr.GetItemText(i, 1); Adesk::UInt16 usColor = (Adesk::UInt16)atoi(str); if (pLine->colorIndex() != usColor) { char *buf = (char*)malloc(20); itoa((int)pLine->colorIndex(), buf, 10); m_ListCtr.SetItemText(i, 1, buf); free(buf); } pLine->close(); } } } } UpdateData(FALSE); acDocManager->unlockDocument(curDoc()); }
void CMyDlg::OnCreateLine() { // TODO: Add your control notification handler code here acDocManager->lockDocument(curDoc()); int i; int m, n; char *buf = (char*)malloc(20); AcDbObjectId LineId; AcDbLine *pLine = NULL; //块表 AcDbBlockTable *pTb = NULL; //块表记录 AcDbBlockTableRecord *pTbr = NULL; //层表 AcDbLayerTable *pLyr = NULL; //层表记录 AcDbLayerTableRecord* pLyrr = NULL; //图形数据库 AcDbDatabase *pDb = acdbHostApplicationServices()->workingDatabase(); Acad::ErrorStatus es; CListCtrl *pListCtr = (CListCtrl *)GetDlgItem( IDC_LIST1 ); //设置随机数种子 srand(time(NULL)); //更新数据 UpdateData(TRUE); m_VecSize += m_Edit5; m_xVec.resize(m_VecSize); m_yVec.resize(m_VecSize); //acutPrintf("%d\n", m_Edit1); es = pDb->getLayerTable(pLyr, AcDb::kForWrite); //以写的方式打开层表 es = pDb->getBlockTable(pTb, AcDb::kForRead); //以读的方式打开块表 es = pTb->getAt(ACDB_MODEL_SPACE, pTbr, AcDb::kForWrite);//以写的方式打开块表记录 //创建图层GalLineTest if (!pLyr->has("GalLineTest")) { pLyrr = new AcDbLayerTableRecord; //创建层记录 pLyrr->setName("GalLineTest"); //设置名字 //pLyrr->setColor(color); //设置颜色 //pLyrr->setLineWeight(lnWt); //设置线宽 if (Acad::eOk != pLyr->add(pLyrr)) //添加该层记录到层表 { //添加失败 delete pLyrr; //释放内存 pLyr->close(); //关闭层表 } //关闭层表记录 pLyrr->close(); } //关闭层表 pLyr->close(); //生成点坐标 for (i = 0; i < m_Edit5; ++i) { if ((m_Edit2 != 0) && (m_Edit4 != 0)) { m_xVec[i] = rand() % m_Edit2 + m_Edit1; m_yVec[i] = rand() % m_Edit4 + m_Edit3; } else { m_xVec[i] = 0; m_yVec[i] = 0; } } //遍历点坐标 for (m = 0; m < m_Edit5; ++m) { for (n = (m+1); n < m_Edit5; ++n) { if ((m_xVec[m] != m_xVec[n]) && (m_yVec[m] != m_yVec[n])) { AcGePoint3d ptStart(m_xVec[m], m_yVec[m], 0); AcGePoint3d ptEnd(m_xVec[n], m_yVec[n], 0); pLine = new AcDbLine(ptStart, ptEnd); //pLine->setColor(); pLine->setLayer("GalLineTest"); //创建线段 es = pTbr->appendAcDbEntity(LineId, pLine); if (Acad::eOk == es) { ++m_lLineCnt; } //acutPrintf("%d\n", LineId); sprintf(buf, "%d", LineId); m_ListCtr.InsertItem(m_Row, buf); buf = itoa(pLine->colorIndex(), buf, 10); m_ListCtr.SetItemText(m_Row, 1, buf); buf = itoa(LineLength(m_xVec[m], m_yVec[m], m_xVec[n], m_yVec[n]), buf, 10); m_ListCtr.SetItemText(m_Row, 2, buf); ++m_Row; pLine->close(); //关闭实体 } } } pTbr->close(); //关闭块表记录 pTb->close(); //关闭块表 acDocManager->unlockDocument(curDoc()); free(buf); }