Author Topic: setVisualStyle make no effect  (Read 673 times)

0 Members and 1 Guest are viewing this topic.

nolanjian

  • Mosquito
  • Posts: 7
setVisualStyle make no effect
« on: September 18, 2017, 02:26:53 AM »
hi, community,
I want to set "Conceptual" visual style to one viewpot, but it makes no effect.
On Autocad, I find that the label on viewport change to "Conceptual", but the entities make no change, still the default style "2D wireframe".
here is my code:
Code: [Select]
void AdjustViewport(const CString& strLayout, int nIndex, const std::vector<AcDbObjectId>& vecEnt, const AcGePoint3d& modelCenter)
{
AcDbViewport* pViewport = GetViewport(strLayout, nIndex);

if (pViewport == nullptr)
{
ASSERT(pViewport != nullptr);
return;
}

int nRetActiveLayout = acedCommand(RTSTR, _T("layout"), RTSTR, _T(""), RTSTR, _T("testCopy"), 0);
ASSERT(nRetActiveLayout == RTNORM);

acedMspace();

Acad::ErrorStatus es = acedSetCurrentVPort(pViewport);
ASSERT(es == Acad::eOk);

int nSetSWISO = acedCommand(RTSTR, _T("-view"), RTSTR, _T("_swiso"), 0);
ASSERT(nSetSWISO == RTNORM);

ads_name ssname;
acedSSAdd( NULL, NULL, ssname );
GroupSelect(vecEnt, ssname);
acedSSSetFirst( ssname, NULL );
int nZOOM = acedCommand(
RTSTR, _T("_ZOOM"),
RTSTR, _T("_OBJECT"),
RTPICKS, ssname,
RTSTR, _T(""),
RTNONE);
ASSERT(nZOOM == RTNORM);
acedSSFree(ssname);
acedSSSetFirst( NULL, NULL );

AcDbObjectId vsId = AcDbObjectId::kNull;
AcDbDictionaryPointer pNOD(acdbHostApplicationServices()->workingDatabase()->visualStyleDictionaryId(), AcDb::kForRead);
pNOD->getAt(ACRX_T("Conceptual"), vsId);

Acad::ErrorStatus esSetVS = pViewport->setVisualStyle(vsId);
ASSERT(esSetVS == Acad::eOk);

//SetVisualStyle(vecEnt);

pViewport->updateDisplay();

pNOD->close();
pViewport->close();
}

pls help, thx!

Bradleyz

  • Mosquito
  • Posts: 2
Re: setVisualStyle make no effect
« Reply #1 on: November 07, 2018, 12:13:55 AM »
Do not know how to help me? About the knowledge I'm looking for, I'm following up on sharing this information.

MickD

  • Gator
  • Posts: 3172
  • I don't need a job, I need Money!!
Re: setVisualStyle make no effect
« Reply #2 on: November 07, 2018, 12:56:25 AM »
Do not know how to help me? About the knowledge I'm looking for, I'm following up on sharing this information.

Just a quick observation:

Could you move the code that changes view settings etc out of this function into its own (that is, all the code that has nothing to do with changing the viewport style) and take another look at what's happening?

something like below (NOTE: not tested!)
Code - C++: [Select]
  1. void SetViewportStyle(const CString& strLayout, int nIndex, string styleName)
  2. {
  3. AcDbViewport* pViewport = GetViewport(strLayout, nIndex); // -------  Where is this function??
  4.  
  5. if (pViewport == nullptr)
  6. {
  7. return;
  8. }
  9.  
  10. AcDbObjectId vsId = AcDbObjectId::kNull;
  11. AcDbDictionaryPointer pNOD(acdbHostApplicationServices()->workingDatabase()->visualStyleDictionaryId(), AcDb::kForRead);
  12. pNOD->getAt(ACRX_T(styleName), vsId);
  13.  
  14.        if (vsId == AcDbObjectId::knull)
  15.        {
  16.              return;
  17.        }
  18.        // ---- shouldn't you open the viewport for write here??
  19. Acad::ErrorStatus esSetVS = pViewport->setVisualStyle(vsId);
  20. if(esSetVS == Acad::eOk)
  21.        {
  22.        pViewport->updateDisplay();
  23.  
  24.        pNOD->close();
  25.        pViewport->close();
  26.        }
  27. }
  28.  

Actually, now looking a bit closer there's a lot fundamentally missing/wrong with this code. You should use a transaction to open the database objects like pNOD etc.
Try to write functions that do one thing at a time, mixing up other tasks in the same function just adds noise and makes it harder to debug. If you need to do a few things then call the smaller functions from one function (say you command function) like this:

void MyCommand(){
    DoThis();
    ThenThis();
    AndThis();
    return;
}
« Last Edit: November 07, 2018, 01:05:07 AM by MickD »
"If you give someone a program, you will frustrate them for a day;
if you teach them how to program, you will frustrate them for a lifetime. "
David Leinweber