tongtong 发表于 2007-2-9 16:46:00

<p>我的程序是通过一个对话框,由用户输入数据的位置和需要移到0图层的图层名称,然后“OK”完成批处理将图形的一些图层的数据移到0图层。下面是主要代码,问题还是依旧是那样的(没有出任何错误,但是也没有执行任何操作,包括最后最后从层表中删除图层也没有执行)大家帮忙看看问题出在哪里啊?谢谢</p><p>void lcdlg::layerclear()&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //主函数<br/>{<br/>&nbsp;&nbsp;if(cPath=="")<br/>&nbsp;&nbsp;cPath= "D:\\temp\\";&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //缺省条件下的数据位置</p><p>&nbsp;CFileFind&nbsp;find;<br/>&nbsp;CFileFind&nbsp;pathfind;<br/>&nbsp;BOOL&nbsp;&nbsp;bFileIsFind;<br/>&nbsp;CString cDWGPath = cPath+"*.dwg";<br/>&nbsp;bFileIsFind = find.FindFile(cDWGPath);<br/>&nbsp;int filecounter=0;<br/>&nbsp;AcDbDatabase *pcurdb= new AcDbDatabase(Adesk::kTrue,Adesk::kTrue);<br/>&nbsp;pcurdb=acdbHostApplicationServices()-&gt;workingDatabase();<br/>&nbsp;CreateDirectory(cPath+"questioned\\",NULL);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//生成一个文件名为questioned的文件夹<br/>&nbsp;CreateDirectory(cPath+"OK\\",NULL);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//生成一个文件名为OK的文件夹<br/>&nbsp;while (bFileIsFind)<br/>&nbsp;{<br/>&nbsp;&nbsp;bAllDone=true;<br/>&nbsp;&nbsp;AcDbDatabase *preaddb= new AcDbDatabase(Adesk::kFalse,Adesk::kTrue);<br/>&nbsp;&nbsp;if(bFileIsFind)<br/>&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;filecounter++;<br/>&nbsp;&nbsp;&nbsp;bFileIsFind = find.FindNextFile();<br/>&nbsp;&nbsp;&nbsp;find.GetFileName();<br/>&nbsp;&nbsp;&nbsp;Acad::ErrorStatus es =preaddb-&gt;readDwgFile(find.GetFilePath(),_SH_DENYNO);<br/>&nbsp;&nbsp;&nbsp;if(es==Acad::eOk)<br/>&nbsp;&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;&nbsp;acdbHostApplicationServices()-&gt;setWorkingDatabase(preaddb);<br/>&nbsp;&nbsp;&nbsp;&nbsp;clear(preaddb,layernames);<br/>&nbsp;&nbsp;&nbsp;&nbsp;if(bAllDone)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;preaddb-&gt;saveAs(cPath+"OK\\"+find.GetFileName());<br/>&nbsp;&nbsp;&nbsp;&nbsp;else<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;preaddb-&gt;saveAs(cPath+"questioned\\"+find.GetFileName());<br/>&nbsp;&nbsp;&nbsp;&nbsp;acdbHostApplicationServices()-&gt;setWorkingDatabase(pcurdb);<br/>&nbsp;&nbsp;&nbsp;&nbsp;delete preaddb;<br/>&nbsp;&nbsp;&nbsp;DeleteFile(find.GetFilePath());<br/>&nbsp;&nbsp;&nbsp;}<br/>&nbsp;&nbsp;&nbsp;else<br/>&nbsp;&nbsp;&nbsp;&nbsp;AfxMessageBox("文件"+find.GetFileName()+"打开出错");<br/>&nbsp;&nbsp;}<br/>&nbsp;}<br/>&nbsp;CString str;<br/>&nbsp;str.Format("%d",filecounter);<br/>&nbsp;AfxMessageBox("修改了"+str+"个文件");</p><p>}</p><p>void lcdlg::clear(AcDbDatabase *pdb,char *names)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //多个需要更改图层的情况<br/>{<br/>&nbsp;AcDbDatabase *pD;<br/>&nbsp;AcDbLayerTable *pT;<br/>&nbsp;pD=pdb;<br/>&nbsp;AcDbObjectIdArray nIDs;<br/>&nbsp;char layername;<br/>&nbsp;int count1=0;<br/>&nbsp;int count2=0;<br/>&nbsp;layername=names;<br/>&nbsp;while (layername!='\0')<br/>&nbsp;{<br/>&nbsp;&nbsp;count2++;<br/>&nbsp;&nbsp;&nbsp;&nbsp;if(layername==';')<br/>&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;while(count1&lt;255)<br/>&nbsp;&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;&nbsp;layername='\0';<br/>&nbsp;&nbsp;&nbsp;&nbsp;count1++;<br/>&nbsp;&nbsp;&nbsp;}<br/>&nbsp;&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;pD-&gt;getSymbolTable(pT, AcDb::kForRead);<br/>&nbsp;&nbsp;&nbsp;if(pT-&gt;has(layername))<br/>&nbsp; &nbsp;&nbsp;&nbsp;delSS(layername,nIDs);</p><p>&nbsp;&nbsp;&nbsp;else<br/>&nbsp;&nbsp;&nbsp;&nbsp;bAllDone=false;<br/>&nbsp;&nbsp;&nbsp;count1=0;<br/>&nbsp;&nbsp;}<br/>&nbsp;&nbsp;else<br/>&nbsp;&nbsp;&nbsp;count1++;<br/>&nbsp;&nbsp;layername=names;<br/>&nbsp;}<br/>}</p><p>void lcdlg::OnOK() //对象框的OK按钮<br/>{<br/>&nbsp;CDialog::OnOK();<br/>&nbsp;sprintf(layernames , "%s",m_layernames);<br/>&nbsp;cPath=m_path;<br/>&nbsp;layerclear();<br/>}</p><p>void lcdlg::delSS(char* nLayerName,AcDbObjectIdArray&amp; nIDs)//将一个图层的数据移到0图层<br/><br/>{<br/>&nbsp;Acad::ErrorStatus es = Acad::eOk;<br/>&nbsp;ads_name ents;<br/>&nbsp;struct resbuf *rb;<br/>&nbsp;struct resbuf blc;<br/>&nbsp;AcDbEntity *pE;</p><p>&nbsp;rb=acutNewRb(AcDb::kDxfLayerName);<br/>&nbsp;rb-&gt;restype=8;<br/>&nbsp;rb-&gt;resval.rstring=(char*)nLayerName;<br/>&nbsp;rb-&gt;rbnext=NULL;<br/>&nbsp;acedSSGet("X",NULL,NULL,rb,ents);</p><p>//&nbsp;long entNums=0;<br/>&nbsp;long entNums;</p><p>&nbsp;acedSSLength(ents,&amp;entNums);<br/>&nbsp;if (entNums == 0)<br/>&nbsp;&nbsp;es = Acad::eInvalidInput;<br/>&nbsp;else<br/>&nbsp;{<br/>&nbsp;&nbsp;for (long a = 0; a &lt; entNums ; a ++)<br/>&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;AcDbObjectId&nbsp; objId;<br/>&nbsp;&nbsp;&nbsp;ads_name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ent;<br/>&nbsp;&nbsp;&nbsp;AcDbObject* obj;<br/>&nbsp;&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;acedSSName(ents,a,ent);<br/>&nbsp;&nbsp;&nbsp;acdbGetObjectId(objId, ent);<br/>&nbsp;&nbsp;&nbsp;acdbOpenObject(pE,objId,AcDb::kForWrite,false);<br/>&nbsp;&nbsp;&nbsp;nIDs.append(objId);</p><p>&nbsp;&nbsp;&nbsp;pE-&gt;setLayer("0");<br/>&nbsp;&nbsp;&nbsp;pE-&gt;close;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; obj-&gt;close;<br/>&nbsp;&nbsp;&nbsp;nIDs.append(objId);<br/>&nbsp;&nbsp;}<br/>&nbsp;}<br/>&nbsp;acedSSFree(ents);<br/>&nbsp;blc.restype = RTSTR;<br/>&nbsp;&nbsp;&nbsp; blc.resval.rstring = "0";<br/>&nbsp;&nbsp;&nbsp; acedSetVar("CLAYER",&amp;blc);//设置当前图层为0层&nbsp; </p><p>&nbsp;AcDbLayerTable * pLayerTbl;//定义层表指针<br/>&nbsp;&nbsp;&nbsp; acdbHostApplicationServices()-&gt;workingDatabase()-&gt;getSymbolTable(pLayerTbl, AcDb::kForWrite);<br/>&nbsp;&nbsp;&nbsp;&nbsp; <br/>&nbsp;&nbsp;&nbsp; AcDbLayerTableRecord * pLayerTblRcd; //定义层表记录指针<br/>&nbsp;&nbsp;&nbsp; if ( Acad::eOk == pLayerTbl-&gt;getAt((char*)nLayerName, pLayerTblRcd , AcDb::kForWrite))<br/>&nbsp;&nbsp;&nbsp; {<br/>&nbsp;&nbsp;&nbsp;&nbsp; pLayerTblRcd-&gt;erase(true);//删除图层<br/>&nbsp;&nbsp;&nbsp; }&nbsp;&nbsp;&nbsp; <br/>&nbsp;&nbsp;&nbsp; pLayerTblRcd-&gt;close(); <br/>&nbsp;&nbsp;&nbsp; pLayerTbl-&gt;close();<br/>&nbsp;&nbsp;&nbsp; acutRelRb(rb);<br/>}</p>

tongtong 发表于 2007-2-12 08:53:00

<p>按照上面的方法现在出现错误提示“Error writting/closing file”,然后把我的测试数据全删掉拉。这是什么问题?</p>

tongtong 发表于 2007-2-12 11:19:00

<p>好的,谢谢大家,已经调试好了。至于上面出错原因我还是不清楚。现将将成功代码公布如下:</p><p>void lcdlg::layerclear()&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //主函数<br/>{<br/>&nbsp;&nbsp;if(cPath=="")<br/>&nbsp;&nbsp;cPath= "D:\\temp\\";&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //缺省条件下的数据位置</p><p>&nbsp;CFileFind&nbsp;find;<br/>&nbsp;CFileFind&nbsp;pathfind;<br/>&nbsp;BOOL&nbsp;&nbsp;bFileIsFind;<br/>&nbsp;CString cDWGPath = cPath+"*.dwg";<br/>&nbsp;bFileIsFind = find.FindFile(cDWGPath);<br/>&nbsp;int filecounter=0;<br/>&nbsp;AcDbDatabase *pcurdb= new AcDbDatabase(Adesk::kTrue,Adesk::kTrue);<br/>&nbsp;pcurdb=acdbHostApplicationServices()-&gt;workingDatabase();<br/>&nbsp;CreateDirectory(cPath+"questioned\\",NULL);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//生成一个文件名为questioned的文件夹<br/>&nbsp;CreateDirectory(cPath+"OK\\",NULL);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//生成一个文件名为OK的文件夹<br/>&nbsp;while (bFileIsFind)<br/>&nbsp;{<br/>&nbsp;&nbsp;bAllDone=true;<br/>&nbsp;&nbsp;AcDbDatabase *preaddb= new AcDbDatabase(Adesk::kFalse,Adesk::kTrue);<br/>&nbsp;&nbsp;if(bFileIsFind)<br/>&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;filecounter++;<br/>&nbsp;&nbsp;&nbsp;bFileIsFind = find.FindNextFile();<br/>&nbsp;&nbsp;&nbsp;find.GetFileName();<br/>&nbsp;&nbsp;&nbsp;Acad::ErrorStatus es =preaddb-&gt;readDwgFile(find.GetFilePath(),_SH_DENYNO);<br/>&nbsp;&nbsp;&nbsp;if(es==Acad::eOk)<br/>&nbsp;&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;&nbsp;acdbHostApplicationServices()-&gt;setWorkingDatabase(preaddb);<br/>&nbsp;&nbsp;&nbsp;&nbsp;clear(preaddb,layernames);<br/>&nbsp;&nbsp;&nbsp;&nbsp;if(bAllDone)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;preaddb-&gt;saveAs(cPath+"OK\\"+find.GetFileName());<br/>&nbsp;&nbsp;&nbsp;&nbsp;else<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;preaddb-&gt;saveAs(cPath+"questioned\\"+find.GetFileName());<br/>&nbsp;&nbsp;&nbsp;&nbsp;acdbHostApplicationServices()-&gt;setWorkingDatabase(pcurdb);<br/>&nbsp;&nbsp;&nbsp;&nbsp;delete preaddb;<br/>&nbsp;&nbsp;&nbsp;DeleteFile(find.GetFilePath());<br/>&nbsp;&nbsp;&nbsp;}<br/>&nbsp;&nbsp;&nbsp;else<br/>&nbsp;&nbsp;&nbsp;&nbsp;AfxMessageBox("文件"+find.GetFileName()+"打开出错");<br/>&nbsp;&nbsp;}<br/>&nbsp;}<br/>&nbsp;CString str;<br/>&nbsp;str.Format("%d",filecounter);<br/>&nbsp;AfxMessageBox("修改了"+str+"个文件");</p><p>}</p><p>void lcdlg::clear(AcDbDatabase *pdb,char *names)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //多个需要更改图层的情况<br/>{<br/>&nbsp;AcDbDatabase *pD;<br/>&nbsp;AcDbLayerTable *pT;<br/>&nbsp;pD=pdb;<br/>&nbsp;AcDbObjectIdArray nIDs;<br/>&nbsp;char layername;<br/>&nbsp;int count1=0;<br/>&nbsp;int count2=0;<br/>&nbsp;layername=names;<br/>&nbsp;while (layername!='\0')<br/>&nbsp;{<br/>&nbsp;&nbsp;count2++;<br/>&nbsp;&nbsp;&nbsp;&nbsp;if(layername==';')<br/>&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;while(count1&lt;255)<br/>&nbsp;&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;&nbsp;layername='\0';<br/>&nbsp;&nbsp;&nbsp;&nbsp;count1++;<br/>&nbsp;&nbsp;&nbsp;}<br/>&nbsp;&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;pD-&gt;getSymbolTable(pT, AcDb::kForRead);<br/>&nbsp;&nbsp;&nbsp;if(pT-&gt;has(layername))<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; delSS(layername);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</p><p>&nbsp;&nbsp;&nbsp;else<br/>&nbsp;&nbsp;&nbsp;&nbsp;bAllDone=false;<br/>&nbsp;&nbsp;&nbsp;count1=0;<br/>&nbsp;&nbsp;}<br/>&nbsp;&nbsp;else<br/>&nbsp;&nbsp;&nbsp;count1++;<br/>&nbsp;&nbsp;layername=names;<br/>&nbsp;}<br/>}</p><p>void lcdlg::OnOK() //对象框的OK按钮<br/>{<br/>&nbsp;CDialog::OnOK();<br/>&nbsp;sprintf(layernames , "%s",m_layernames);<br/>&nbsp;cPath=m_path;<br/>&nbsp;layerclear();<br/>}</p><p>void lcdlg::delSS(const char *sLay)<br/>{<br/>&nbsp;ads_name ss;<br/>&nbsp;struct resbuf *rb=acutBuildList(8,sLay,NULL);<br/>&nbsp;long nLen=0;<br/>&nbsp;if(acedSSGet("X",NULL,NULL,rb,ss)!=RTNORM)<br/>&nbsp;{<br/>&nbsp; acutRelRb(rb);<br/>&nbsp; return;<br/>&nbsp;}<br/>&nbsp; acutRelRb(rb);</p><p>&nbsp;acedSSLength(ss,&amp;nLen);<br/>&nbsp;for(long n=0L;n&lt;nLen;n++)<br/>&nbsp;{<br/>&nbsp; ads_name ent;<br/>&nbsp; acedSSName(ss,n,ent);<br/>&nbsp; AcDbObjectId idEnt;<br/>&nbsp; acdbGetObjectId(idEnt,ent);<br/>&nbsp; AcDbEntity *pEnt=NULL;<br/>&nbsp; if(acdbOpenObject(pEnt,idEnt,AcDb::kForWrite)==Acad::eOk)<br/>&nbsp; {<br/>&nbsp;&nbsp; pEnt-&gt;setLayer("0");<br/>&nbsp;&nbsp; pEnt-&gt;close();<br/>&nbsp; }<br/>&nbsp;}<br/>&nbsp;acedSSFree(ss);<br/>}</p>

tongtong 发表于 2007-2-28 10:36:00

<p>如果我还想继续把已经移到0层的图层删除是怎么做的啊?我是这样做的,但是有问题,请指教</p><p>void lcdlg::delSS(const char *sLay)<br/>{<br/>&nbsp;ads_name ss;<br/>&nbsp;struct resbuf *rb=acutBuildList(8,sLay,NULL);<br/>&nbsp;long nLen=0;<br/>&nbsp;if(acedSSGet("X",NULL,NULL,rb,ss)!=RTNORM)<br/>&nbsp;{<br/>&nbsp; acutRelRb(rb);<br/>&nbsp; return;<br/>&nbsp;}<br/>&nbsp; acutRelRb(rb);</p><p>&nbsp;acedSSLength(ss,&amp;nLen);<br/>&nbsp;for(long n=0L;n&lt;nLen;n++)<br/>&nbsp;{<br/>&nbsp; ads_name ent;<br/>&nbsp; acedSSName(ss,n,ent);<br/>&nbsp; AcDbObjectId idEnt;<br/>&nbsp; acdbGetObjectId(idEnt,ent);<br/>&nbsp; AcDbEntity *pEnt=NULL;<br/>&nbsp; if(acdbOpenObject(pEnt,idEnt,AcDb::kForWrite)==Acad::eOk)<br/>&nbsp; {<br/>&nbsp;&nbsp; pEnt-&gt;setLayer("0");<br/>&nbsp;&nbsp; pEnt-&gt;close();<br/>&nbsp; }<br/>&nbsp;}<br/>&nbsp;acedSSFree(ss);</p><p>&nbsp;<br/>&nbsp;<font color="#ff3300">AcDbLayerTable * pLayerTbl;//定义层表指针<br/>&nbsp;&nbsp;&nbsp; acdbHostApplicationServices()-&gt;workingDatabase()-&gt;getSymbolTable(pLayerTbl, AcDb::kForWrite);<br/>&nbsp;&nbsp;&nbsp;&nbsp; <br/>&nbsp;&nbsp;&nbsp; AcDbLayerTableRecord * pLayerTblRcd; //定义层表记录指针<br/>&nbsp;&nbsp;&nbsp; if ( Acad::eOk == pLayerTbl-&gt;getAt(sLay, pLayerTblRcd , AcDb::kForWrite))<br/>&nbsp;&nbsp;&nbsp; {<br/>&nbsp;&nbsp;&nbsp;&nbsp; pLayerTblRcd-&gt;erase(true);//删除图层<br/>&nbsp;&nbsp;&nbsp; }&nbsp;&nbsp;&nbsp; <br/>&nbsp;&nbsp;&nbsp; pLayerTblRcd-&gt;close(); <br/>&nbsp;pLayerTbl-&gt;close();<br/>&nbsp;free(&amp;sLay);<br/></font>&nbsp;<br/>}<br/></p>
页: 1 [2]
查看完整版本: 批量把所有图层放入到0层