高级会员
注册日期: 06-11
帖子: 14579
精华: 1
现金: 224494 标准币
资产: 234494 标准币
|
【转帖】attributes in a block a not shown at the correct position
attributes in a block a not shown at the correct position
problem positioning attributes which belong to a block
hello ,
i want to copy a block of an existing drawing containing attributes and insert it as a definition and as a blockreference in a new drawing. when doing so with the code below, positionning geomerical elements works fine. however, the attributes are always positioned around the origin.
what i want to do in detail:
1. insert a block containing lines and attributes from a dxf drawing (=drawinga) to another dxf drawing (drawingb) as a block definition.
2. create a block instance (block record) of this block at a new position in drawingb.
my problem:
1. i can insert the block definition in drawingb including the attribute definitions
2. i can create a block instance and insert it at a given position (posx, posy). but now the problem: when i insert the block, the attributes will always be printed around the origin (0,0), whereas the geometrical elements of the block will be printed at the correct position. see the attached .dxf file for an example (if you try to insert the a blockreference of the block "basic" using autocad, everything works fine).
the function for inserting a block definition in drawingb is:
int insertblockfromdrawingasblockdefinition(const char* srcfilename, const char* srcblockname){
//opens the source database
oddbdatabaseptr psrcdb = opendatabase(srcfilename);
//add the block with name srcblockname to the destination db.
//this creates the block to the db
oddbobjectid blockdefidindb = pdb->insert(srcblockname,srcblockname, psrcdb, true);
//release the source db
psrcdb.release();
psrcdb = 0;
//add it the id to the array and return the index
return pobjarr->append(blockdefidindb);
}
to create an instance of the block in drawingb i use the following function:
int insertblockreferenceinmodelspace(
int blockdefinitionid, // id of block to be inserted
double xposition, double yposition, double rotation, double scale) {
//get the model space "block"
oddbobjectid blocktableid = pdb->getblocktableid();
oddbsymboltableptr pblocktable = blocktableid.safeopenobject();
oddbblocktablerecordptr pmodelspaceblock = pblocktable->getat("*model_space", oddb::kforwrite);
// create the block instance which will be inserted)
oddbblockreferenceptr pblockref = oddbblockreference::createobject();
// add the entity to the parent block (=model space block).
oddbobjectid blockrefid = pmodelspaceblock->appendoddbentity(pblockref);
// get the block defintion from the blocktablerecord
pblockref->setblocktablerecord(pobjarr->at(blockdefinitionid));
//set the properties
pblockref->setscalefactors(odgescale3d(scale, scale, 1.0));
pblockref->setposition(odgepoint3d(xposition, yposition, 0));
pblockref->setrotation(rotation *(m_pi / 180.0));
//add the attributes
oddbblocktablerecordptr pblockdefinition = pobjarr->at(blockdefinitionid).safeopenobject();
addattributestoblockreference(pblockref, pblockdefinition);
//add it the id to the array and return the index
return pobjarr->append(blockrefid);
}
the following is only a helper function, which creates the attributes in the blockreference using the corresponding attribute definitions:
void addattributestoblockreference(oddbblockreferencept r pblockref, oddbblocktablerecordptr pblockdefinition){
oddbobjectiteratorptr piter = pblockdefinition->newiterator();
for(piter->start(); !piter->done(); piter->step())
{
oddbentityptr pentity = piter->entity();
oddbattributedefinitionptr pattributedef = oddbattributedefinition::cast(pentity);
//add all attributes of this block's definition
//to the block reference (= block instance
if(!pattributedef.isnull())
{
oddbattributeptr pattribute = oddbattribute::createobject();
//append the attribute the the block reference
pblockref->appendattribute(pattribute);
pattribute->setpropertiesfrom (pattributedef, true);
pattribute->settag(pattributedef->tag());
pattribute->settextstring(pattributedef->textstring());
pattribute->setalignmentpoint (pattributedef->alignmentpoint());
pattribute->setheight (pattributedef->height());
pattribute->sethorizontalmode (pattributedef->horizontalmode());
pattribute->setnormal (pattributedef->normal());
pattribute->setoblique (pattributedef->oblique());
pattribute->setposition (pattributedef->position());
pattribute->setrotation (pattributedef->rotation());
pattribute->settextstring (pattributedef->textstring());
pattribute->settextstyle (pattributedef->textstyle());
pattribute->setwidthfactor (pattributedef->widthfactor());
}
}
}
cheers and thanks for any help,
christoph
attached files
you set the position of the attribute to be the same as the position of the definition.
you may want to add the position of the insert to it.
vladimir
hi valdimir,
that's true. but even when i replace the code in addattributestoblockreference(..) fff">to the following, it does not work:
void addattributestoblockreference(oddbblockreferencept r pblockref, oddbblocktablerecordptr pblockdefinition){
oddbobjectiteratorptr piter = pblockdefinition->newiterator();
for(piter->start(); !piter->done(); piter->step())
{
oddbentityptr pentity = piter->entity();
oddbattributedefinitionptr pattributedef = oddbattributedefinition::cast(pentity);
//add all attributes of this block's definition
//to the block reference (= block instance
if(!pattributedef.isnull())
{
oddbattributeptr pattribute = oddbattribute::createobject();
//append the attribute the the block reference
pblockref->appendattribute(pattribute);
pattribute->setpropertiesfrom (pattributedef, true);
pattribute->settag(pattributedef->tag());
pattribute->settextstring(pattributedef->textstring());
pattribute->setalignmentpoint (pattributedef->alignmentpoint());
pattribute->setheight (pattributedef->height());
pattribute->sethorizontalmode (pattributedef->horizontalmode());
pattribute->setnormal (pattributedef->normal());
pattribute->setoblique (pattributedef->oblique());
pattribute->setposition (odgepoint3d(pattributedef->position().x+ pblockref->position().x, pattributedef->position().y + pblockref->position().y ,0));
pattribute->setrotation (pattributedef->rotation());
pattribute->settextstring (pattributedef->textstring());
pattribute->settextstyle (pattributedef->textstyle());
pattribute->setwidthfactor (pattributedef->widthfactor());
}
}
}
well, it is easier just to apply block transform to the attributes. did you look at writeex sample?
vladimir
hi valdimir,
i looked at dbfiller::addblockdef(..).
there is a function call:
patt->transformby (blkxfm);
i added this to my function and it works!
there is one more question which i have now: i do the positioning of the blockref using:
pblockref->setposition(odgepoint3d(xposition, yposition, 0));
should i use
pblockref->transformby(blkxfm);
instead? or does it make no difference?
cheers and thanks a lot,
christoph
i think, it makes no difference
vladimir
hi vladimir,
i was able to set a block to a new location and to rotate a block using:
pblockref->setposition(odgepoint3d(xposition, yposition, 0));
pblockref->setrotation(odatoradian(rotation));
to translate an attribute and to rotate it, i needed to do the following (mix between odgematrixfff"> and pattribute->setrotation()fff">):
odgematrix3d blkxfm;
blkxfm.settranslation(pblockref->position().asvector());
pattribute->setrotation(pblockref->rotation());
apply the translation to the attribute
pattribute->transformby (blkxfm);
doing everything with odgematrix3d did not work. especially to call pblockreference->transformby(blkxfm)fff"> does not seem to have any effect! could there be an error related to that in the library? (the code examples below don't seem to have any effect!)
//rotate and translate the block
odgematrix3d blkxfm;
blkxfm.translation(odgepoint3d(xposition, yposition, 0).asvector());
blkxfm.rotation(odatoradian(rotation), odgevector3d.kzaxis, odgepoint3d(0, 0, 0));
pblockref->transformby(blkxfm);
//rotate and translate the attribute
odgematrix3d blkxfm;
blkxfm.rotation(pblockref->rotation(), odgevector3d.kzaxis, odgepoint3d.korigin);
blkxfm.settranslation(pblockref->position().asvector());
pattribute->transformby (blkxfm);
thanks for any help,
christoph
odgematrix3d::translation
odgematrix3d::rotation
are static methods returning rotation and translation matrices
vladimir
in the meantime i also found it out.
sorry for having disturbed you again...
cheers and thanks,
christoph
|