为什么我不能成功创建组?谢谢(转)
www.dimcax.com
为什么我不能成功创建组?谢谢(转)
我想在以下函数中创建一个组,把一个圆和直线作为一个块对象添加到图形中,但是如果去掉有关创建组的代码时,能正确的把直线和圆添加到图形中,加上了创建组的代码后,却总是提示:创建组失败。代码如下: [commandmethod("createtest")] public void createemployeetest() { database db = hostapplicationservices.workingdatabase; objectid layerid = entityhelper.createlayer("employeelayer", 1); //调用函数创建新的层 objectidcollection coll = new objectidcollection(); //定义一个objectid的集合,用来加入直线和圆的objectid objectid enid = entityhelper.createline(new point3d(10, 10, 0), new point3d(20, 20, 0)); objectid gpid = objectid.null; group gp = new group("employeegroup", true); try { if (enid != objectid.null) { coll.add(enid); } enid = entityhelper.createcircle(new point3d(10, 10, 0), vector3d.zaxis, 2); if (enid != objectid.null) { coll.add(enid); } int last = coll.count; //获取coll集合的个数 using (transaction trans = db.transactionmanager.starttransaction()) { try { dbdictionary dict = (dbdictionary)trans.getobject(db.groupdictionaryid, openmode.forwrite, false); //如果已有同名组,则返回原有同名组对象 gpid = dict.getat("employeegroup"); if (gpid != objectid.null) { ed.writemessage("组已存在"); trans.abort(); trans.dispose(); return; } gpid = dict.setat("employeegroup", gp); foreach (objectid id in coll) { gp.append(id); } trans.addnewlycreateddbobject(gp, true); trans.commit(); } catch { gpid = objectid.null; ed.writemessage("创建组失败"); } finally { trans.dispose(); } } } catch (system.exception exp) { ed.writemessage("createemployee failed:" + exp.message); } } 这个代码是根据autodesk的教程和才鸟老大的教程组合起来的,在entityhelper类中写了一些静态方法用于处理一些基本的图形实体,在这些方法中创建图形实体时,已进行了提交。谢谢各位指点
问题好象出在你没有把创建组这事处理完,就把实体id放进去。
这两个过程我在使用,给你参考,我创建的是匿名组
#region "群组"
''' <summary>实体id加入到群组。</summary>
''' <param name="objectids">id组数.</param>
''' <param name="description">群组说明.</param>
public shared function objiectidtogroup(byval objectids() as objectid, byval description as string) as objectid
dim db as database = application.documentmanager.mdiactivedocument.database
using trans as transaction = db.transactionmanager.starttransaction()
dim mygroup as group = trans.getobject(creategroup(description), openmode.forwrite, true)
for each id as objectid in objectids
mygroup.append(id)
next
trans.commit()
end using
end function
private shared function creategroup(byval description as string) as objectid
dim db as database = hostapplicationservices.workingdatabase()
dim mygroup1 as group
using trans as transaction = db.transactionmanager.starttransaction()
dim dict as dbdictionary = trans.getobject(db.groupdictionaryid, openmode.forwrite, true)
try
mygroup1 = trans.getobject(dict.getat(description), openmode.forread)
catch
mygroup1 = new group(description, true)
dict.setat(description, mygroup1)
mygroup1.setanonymous()
trans.addnewlycreateddbobject(mygroup1, true)
end try
trans.commit()
end using
return mygroup1.objectid
end function
#end region
复制代码