高级会员
注册日期: 06-11
帖子: 14579
精华: 1
现金: 224494 标准币
资产: 234494 标准币
|
【转帖】set Block Attribute Values
set block attribute values ...
set block attribute values ...
attributes in autocad block:
i want to assign values to the attributes in a blockreference (insert).
please let me know the best way that i could follow using the dwgdirect library functions to get access to the blockreference object and modify its attribute values.
other details:
dwgdirect 1.10, 1/23/2004
autocad 2002
vc++ 6.0
windows 2000 pro
more info
since no one responded yet, i assume more info is required. it seems like this should be easy to do, yet for some reason i am not getting the expected results ...
query:
i am trying to assign new value to the attributes in a block insert (block reference). find below the part of code which i am using.
can someone tell me why this code is not updating the attribute values in the block.
thanx in advance!
----------------------------------------------------------
void updateblocks(oddbblocktablerecordptr pblockptr) {
// get an entity iterator
oddbobjectiteratorptr pentiter = pblockptr->newiterator();
// for each attribute in the block
for(; !pentiter->done(); pentiter->step())
{
// open the entity
oddbentityptr pent = pentiter->entity(oddb::kforwrite);
if (pent->iskindof(oddbattributedefinition::desc()))
{
oddbattributedefinitionptr pattdef = pent;
pattdef->settextstring("new value");
}
}
}
your code is attempting to modify attribute definitions inside the block not attributes of block reference.
to get attributes of block reference use oddbblocktablereference::attributesiterator().
sergey slezkin
block attributes
quote:
originally posted by sergey slezkin
your code is attempting to modify attribute definitions inside the block not attributes of block reference.
to get attributes of block reference use oddbblocktablereference::attributesiterator().
thanx for the reply.
i guess you are mentioning about oddbblockreference::attributeiterator().
i am unable to get the pointer to blockreference from the block table record.
find below the code which i am using for iterating through the block table.
--------------------------------------------------------
oddbblocktableptr pblocks = pdb->getblocktableid().safeopenobject(oddb::kforwrite) ;
// get an iterator for the block table
oddbblocktableiteratorptr pblkiter = pblocks->newiterator();
oddbblocktablerecordptr pblockptr;
// for each block in the block table
for (pblkiter->start(); ! pblkiter->done(); pblkiter->step())
{
pblockptr = pblkiter->getrecord(oddb::kforwrite);
if (!strcmpi("*model_space", pblockptr->getname()) || !strcmpi("*paper_space", pblockptr->getname())) continue;
if (false == pblockptr->hasattributedefinitions()) continue;
updateblocks(pblockptr);
}
--------------------------------------------------------------
block definition (oddbblocktablerecord) may contain attribute definitions (oddbattributedefinition). block insertion (oddbblockreference) may have attribute values attached (oddbattribute).
each block (oddbblocktablerecord) may be inserted multiple times with different attribute values (oddbattribute entities attached to oddbblockreference).
in the first post you wrote that you you want to alter attributes of block reference.
in the last post you attached code modifying attribute definition.
block reference entity can be found while iterating model, paper spaces (or other blocks).
another method is using oddbblocktablerecord::getblockreferenceids(). it will return array of oddbblockreference ids referencing the oddbblocktablerecord.
sergey slezkin
block attribute (insert)
quote:
originally posted by sergey slezkin
block definition (oddbblocktablerecord) may contain attribute definitions (oddbattributedefinition). block insertion (oddbblockreference) may have attribute values attached (oddbattribute).
each block (oddbblocktablerecord) may be inserted multiple times with different attribute values (oddbattribute entities attached to oddbblockreference).
in the first post you wrote that you you want to alter attributes of block reference.
in the last post you attached code modifying attribute definition.
block reference entity can be found while iterating model, paper spaces (or other blocks).
another method is using oddbblocktablerecord::getblockreferenceids(). it will return array of oddbblockreference ids referencing the oddbblocktablerecord.
thanx for the suggestion.
yes, i want to update the value of te attribute in the insert and not definition.
i am unable to get the pointer to blockreference. also i tried using oddbblocktablerecord::getblockreferenceids() but the array was empty.
can you please send me the sample code to get the referenceids from the blocktable record...
find below the code which i am using for getting the referenceids
--------------------------------
oddbblocktableptr pblocks = pdb->getblocktableid().safeopenobject(oddb::kforwrite) ;
oddbblocktableiteratorptr pblkiter = pblocks->newiterator();
oddbblocktablerecordptr pblockptr;
oddbobjectidarray objarray;
oddbobjectid blkid;
// for each block in the block table
for (pblkiter->start(); ! pblkiter->done(); pblkiter->step())
{
pblockptr = pblkiter->getrecordid().safeopenobject(oddb::kforwrite);
pblockptr->getblockreferenceids(objarray);
if (objarray.isempty() == true) return;
blkid = objarray.getat(0);
}
due to dd bug getreferenceids() reurns empty array for loaded pre-r15 files. it should work for r15+ files. calling oddbdatabase::audit() fixes the problem.
sergey slezkin
set block reference attribute values
quote:
originally posted by sergey slezkin
due to dd bug getreferenceids() reurns empty array for loaded pre-r15 files. it should work for r15+ files. calling oddbdatabase::audit() fixes the problem.
thanx again...
i am working with autocad 2002 drawing files.
i did try with oddbdatabase::audit() as given below
------------
oddbauditinfo pinfo;
pinfo.setfixerrors(true);
pdb->auditdatabase(&pinfo);
-----------
even with the above lines in the code the getblockreferenceid() is empty.
please let me know how i could solve this problem....
i am unable to find some sample code which does update of blockreference attrib values.
are you sure that the block record returning empty array from getreferenceids() is really inserted in the drawing? maybe where are no block references of this block?
sergey slezkin
set block reference attributes
quote:
originally posted by sergey slezkin
are you sure that the block record returning empty array from getreferenceids() is really inserted in the drawing? maybe where are no block references of this block?
thanx once again....
finally i am able to get the pointer to block reference and assign values to the attributes...
here is the code which i have used...
let me know if i can improve this code further and optimize....
-------------------------
oddbblocktablerecordptr pblkrec = pdb->getmodelspaceid().openobject(oddb::kforwrite);;
oddbobjectiteratorptr pblkreciter = pblkrec->newiterator();
for (pblkreciter->start(); !pblkreciter->done(); pblkreciter->step())
{
oddbentityptr pentity = pblkreciter->objectid().safeopenobject();
oddbblockreferenceptr pblkref;
if (pentity->iskindof(oddbblockreference::desc()))
pblkref = pentity->objectid().openobject(oddb::kforwrite);
else continue;
oddbobjectiteratorptr pattiter = pblkref->attributeiterator();
for(; !pattiter->done(); pattiter->step())
{
// open the entity
oddbentityptr pent = pattiter->objectid().openobject(oddb::kforread);
if (pent->iskindof(oddbattribute::desc()))
{
oddbattributeptr pattdef = pblkref->openattribute(pattiter->objectid(), oddb::kforwrite);
pattdef->settextstring("testvalue");
}
}
i modified a bit your code to make it more compact.
but in such case you iterate through the all model space entities (where may be thousands of them). you probably need to iterate through paper spaces as well. and maybe through other blocks too (the block you are interested in may be inserted not into model space but into another block which is inserted into model or paper space (nested).
if you from the very beginning know the name of block which insertions should be altered (attributes modified) the shortest way is to get block reference ids from block table record.
oddbblocktablerecord::getreferenceids() should work if the block is realy inserted.
could you post to forum or e-mail me (see my profile) the file for which getreferenceids() does not work?
code:
oddbblocktablerecordptr pblkrec = pdb->getmodelspaceid().openobject(); //no need in opening for write
oddbobjectiteratorptr pblkreciter = pblkrec->newiterator();
for (pblkreciter->start(); !pblkreciter->done(); pblkreciter->step())
{
oddbblocreferenceptr pblkref = oddbblockreference::cast(pblkreciter->entity());
if (pblkref.isnull())
{ // not a block reference
continue;
}
oddbobjectiteratorptr pattiter = pblkref->attributeiterator();
for(; !pattiter->done(); pattiter->step())
{ // open the entity
oddbattributeptr pattdef = pattiter->entity(oddb::kforwrite);
pattdef->settextstring("testvalue");
}
}
sergey slezkin
|