【MSCE C++ UPDATE 13】生成彩色mesh的问题

我按照论坛上的代码可以生成一个彩色的mesh。我现在想基于一个已经存在的mesh,给这个mesh'节点上添加颜色,我用了下面的代码,编译通过,但是运行起来就直接崩溃。请教这种需求可以怎么实现?

ElementId elmID = 1977L;
	EditElementHandle eeh(elmID, ACTIVEMODEL);
	PolyfaceHeaderPtr polyface;
	MeshHeaderHandler* meshHeaderHandler = dynamic_cast<MeshHeaderHandler*>(eeh.GetDisplayHandler());
	meshHeaderHandler->GetMeshData(eeh, polyface);
	BlockedVectorDPoint3dR xyzPts = polyface->Point();
	bvector<DPoint3d> xyzPts1;
	for (size_t i = 0; i < xyzPts.size(); i++)
	{
		xyzPts1.push_back(xyzPts[i]);
	}
	BlockedVectorIntR indices = polyface->PointIndex();
	//bvector<int> indices1;
	int indices1[8];
	for (size_t i = 0; i < indices.size(); i++)
	{
		indices1[i] = indices[i];
	}

	int vertexClrs[] = { RGB(255, 0, 0), RGB(0, 255, 0), RGB(0, 0, 255), RGB(255, 255, 0), RGB(120, 255, 0),
		RGB(255, 0, 0), RGB(255, 255, 0), RGB(0, 255, 255), RGB(0, 255, 0), RGB(120, 160, 0) };
	
	polyface->IntColor().insert(polyface->IntColor().begin(), vertexClrs, vertexClrs + sizeof(vertexClrs) / sizeof(int));
	polyface->IntColor().SetActive(true);
	polyface->ColorIndex().insert(polyface->ColorIndex().begin(), indices1, indices1 + sizeof(indices1) / sizeof(int));
	polyface->ColorIndex().SetActive(true);


	EditElementHandle  eehColored;
	DgnModelP          pActiveModel = ISessionMgr::GetActiveDgnModelP();
	if (SUCCESS == MeshHeaderHandler::CreateMeshElement(eehColored, NULL, *polyface, true, *pActiveModel))
	{
		eehColored.AddToModel();
	}

  • 请参考下面代码给mesh顶点附加颜色:

    	DPoint3d  xyzPts[] = { { -57735, 0, -81649 },{ -57735, 0, 81649 },{ 57735, -81649, 0 },{ 57735, 81649, 0 } };
    	int   vertexClrs[] = { RGB(255, 0, 0), RGB(0, 255, 0), RGB(0, 0, 255), RGB(255, 255, 0) };
    	          //Face= 1,       2,       3,       4
    	int indices[] = { 1, 2, 4, 2, 3, 4, 3, 2, 1, 1, 4, 3 };
    
    	PolyfaceHeaderPtr pMesh = PolyfaceHeader::CreateFixedBlockIndexed(3);
    	pMesh->Point().insert(pMesh->Point().begin(), xyzPts, xyzPts + sizeof(xyzPts) / sizeof(DPoint3d));
    	pMesh->Point().SetActive(true);
    	pMesh->PointIndex().insert(pMesh->PointIndex().begin(), indices, indices + sizeof(indices) / sizeof(int));
    	pMesh->PointIndex().SetActive(true);
    
    	pMesh->IntColor().insert(pMesh->IntColor().begin(), vertexClrs, vertexClrs + sizeof(vertexClrs) / sizeof(int));
    	pMesh->IntColor().SetActive(true);
    	pMesh->ColorIndex().insert(pMesh->ColorIndex().begin(), indices, indices + sizeof(indices) / sizeof(int));
    	pMesh->ColorIndex().SetActive(true);
    
    	EditElementHandle  eeh;
    	DgnModelP          pActiveModel = ISessionMgr::GetActiveDgnModelP();
    	if (SUCCESS == MeshHeaderHandler::CreateMeshElement(eeh, NULL, *pMesh, true, *pActiveModel))
    	{
    		eeh.AddToModel();
    	}

  • 张老师你好,我就是基于这段代码写的,但是您这段代码是创建一个新元素,我想在一个已经存在的mesh上给节点添加颜色。

    Work smart, not just work hard!

  • 在已有的Mesh上添加的话,获取一下Mesh元素的PolyfaceHeaderPtr,然后在获取的PolyfaceHeaderPtr上调用IntColor,ColorIndex,然后再用这个PolyfaceHeaderPtr创建新的元素替换掉原有元素,您试试这个办法。

    Answer Verified By: Grant Zhang