problems with copying raster images
problems with copying raster images
hi,
here is a sample code which is running in odamfxapp, dwgdirect 1.14.02.
code:
oddbobjectid iddim,iddim2,iddim3,iddim4,iddim5,idline,idline2,idline3,idline4,idline5,idref,idref2;
oddbobjectid idblock;;
oddbdatabase* pdb = getdocument()->m_pdb;
// place your bitmap here
odstring sfile = "d:\\aspire2.bmp";
/* load the raster image */
odgirasterimageptr rast;
oddbobjectid imagedefid;
{
typedef odsmartptr<odrxrasterservices> odrxrasterservicesptr;
odrxrasterservicesptr prassvcs = odrxdynamiclinker()->loadapp(rx_raster_services_appname);
if ( !prassvcs.isnull() )
rast = prassvcs->loadrasterimage(sfile);
}
oddbblocktablerecordptr bbtr = pdb->getactivelayoutbtrid().safeopenobject(oddb::kforwrite);
/* set the origin point */
odgepoint3d point = odgepoint3d(50.0,50.0,0.0);
double h = 100.0;
double w = 100.0;
point.y -= h;
{
/* open the image dictionary */
oddbobjectid imagedictid = oddbrasterimagedef::createimagedictionary(pdb);
oddbdictionaryptr pimagedict = imagedictid.safeopenobject(oddb::kforwrite);
/* create an imagedef object */
oddbrasterimagedefptr pimagedef = oddbrasterimagedef::createobject();
// check raster variables
oddbrastervariablesptr prvars = oddbrastervariables:

penrastervariables(pdb, oddb::kforwrite);
if (odgirasterimage::knone == prvars->userscale())
{
prvars->setuserscale(odgirasterimage::kmeter);
prvars->setimageframe(oddbrastervariables::kimageframeabove);
prvars->setimagequality(oddbrastervariables::kimagequalityhigh);
}
imagedefid = pimagedict->setat("aspire2", pimagedef);
/* set some parameters */
pimagedef->setsourcefilename(sfile);
pimagedef->setimage(rast);
}
oddbrasterimageptr pimage;
{
/* create an image object */
pimage = oddbrasterimage::createobject();
bbtr->appendoddbentity(pimage);
/* set some parameters */
pimage->setimagedefid(imagedefid);
pimage->setorientation(point, odgevector3d(w, 0, 0), odgevector3d(0.0, h, 0));
pimage->setdisplayopt(oddbrasterimage::kshow, true);
pimage->setdisplayopt(oddbrasterimage::kshowunaligned, true);
//pimage->setbrightness(3);
pimage->setdisplayopt(oddbrasterimage::kshow,true);
pimage->setdisplayopt(oddbrasterimage::kshowunaligned ,true);
pimage->setdisplayopt(oddbrasterimage::kclip ,true);
}
// write the raster image to file
pdb->writefile("d:\\test.dwg", oddb::kdwg, oddb::kdhl_1800, theapp.getsavepreview());
pdb = theapp->createdatabase();
{
//copy the raster image from the temp file to the database;
oddbdatabaseptr psenddb = theapp.readfile("d:\\test.dwg", true, false, oda::ksharedenyno );
oddbblocktablerecordptr psendmodelspace = psenddb->getactivelayoutbtrid().safeopenobject();
oddbobjectidarray ids;
oddbrasterimageptr praster;
oddbobjectiteratorptr psendit = psendmodelspace->newiterator();
for (; !psendit->done(); psendit->step()) {
praster = oddbrasterimage::cast(psendit->objectid().safeopenobject());
if(!praster.isnull())
ids.push_back(praster->objectid());
} //for
{
oddbidmappingptr pmapping = oddbidmapping::createobject();
oddbobjectid owner = pdb->getmodelspaceid();
pmapping->setdestdb(pdb);
psenddb->wblockcloneobjects(ids,owner,*pmapping,(oddb::duplicaterecordcloning)/*oddb::kdrcmanglename*/oddb::kdrcmanglename,false);
//move the copied raster image(to be able to see the second opject)
oddbidpair pp(praster->objectid());
if (pmapping->compute(pp)) {
oddbrasterimageptr primage= pp.value().safeopenobject(oddb::kforwrite);
odgematrix3d m;
m.settoidentity();
m.settranslation(odgepoint3d(0.0,0.0,0.0)-odgepoint3d(150.0,150.0,0.0));
primage->transformby(m);
} //if
}
//copy the raster image agan(make a second copy)
{
oddbidmappingptr pmapping1 = oddbidmapping::createobject();
oddbobjectid owner = pdb->getmodelspaceid();
pmapping1->setdestdb(pdb);
psenddb->wblockcloneobjects(ids,owner,*pmapping1,(oddb::duplicaterecordcloning)oddb::kdrcmanglename,false);
}
pdb->writefile("d:\\outputfile.dwg", oddb::kdwg, oddb::kdhl_1800, theapp.getsavepreview());
//now open the output drawing and see the diference. one of the raster image copies is transparent.
}
the problem is that after copying the raster images to another drawing, one of the remains blank - the raster image def is set to null.
is it a bug of dwgdirect?
thanks in advance for any help.
best regards
chudomir
at the moment dd does not map image dictionary by default. it will be fixed in the next maintenance release.
as workaround you can set odrxeventreactor which makes such mapping:
code:
class odwblockrasterimagereactor : public odstaticrxobject<odrxeventreactor>
{
public:
virtual void beginwblockobjects(oddbdatabase* psrc, oddbidmapping& idmap)
{
oddbdatabase* pdst = idmap.destdb();
oda_assert(psrc && pdst);
if (psrc == pdst)
return;
oddbobjectid srcdicid = oddbrasterimagedef::imagedictionary(psrc);
oddbobjectid dstdicid = oddbrasterimagedef::imagedictionary(pdst);
if (!srcdicid.isnull() && !dstdicid.isnull())
{
idmap.assign(oddbidpair(srcdicid, dstdicid, true));
}
}
};
odwblockrasterimagereactor wblockreactor;
odrxevent()->addreactor(&wblockreactor);
//
// ... using wblockcloneobjects()
//
regards,
sergey vishnevetsky
thanks! i'll try.
best regards
chudomir