[Connect CPP] Create label programmaticaly

Hi

I've made a function that can be used to place a label with data taken from Item Types. It is listed below:

BentleyStatus lifalib_placeLabel(ElementRefP elmRefIn, char *cellName, double scale, DPoint3d ptOrigin, double offsetX, double offsetY, bool placeLeader, double rotAngleInDregrees = 0.0, UInt32 levelID = 0)
{
	MSElementDescr *pedCell;
	DPoint3d		ptScale, ptTemp;
	wchar_t			wcCellName[MAX_CELLNAME_LENGTH];
	BentleyStatus	rc = SUCCESS;

	EditElementHandle eeh(elmRefIn);

	ptTemp = ptOrigin;
	ptTemp.x -= offsetX;
	ptTemp.y -= offsetY;

	mbstowcs(wcCellName, cellName, MAX_CELLNAME_LENGTH);
	ptScale.x = ptScale.y = ptScale.z = scale;
	if (mdlCell_getElmDscr(&pedCell, NULL, &ptOrigin, &ptScale, true, NULL, NULL, NULL, 0, true, wcCellName, NULL) == SUCCESS)
	{
		ElementHandle ehCell(pedCell, true, true, ISessionMgr::GetActiveDgnModelRefP());
		EditElementHandle labelElm, leaderElm;
		DimensionStylePtr dimStylePtr = DimensionStyle::GetActive();

		// set terminator size
		double arrowSizeSave;
		dimStylePtr->GetDoubleProp(arrowSizeSave, DimStyleProp::DIMSTYLE_PROP_Terminator_Width_DOUBLE);
		dimStylePtr->SetDoubleProp(scale, DimStyleProp::DIMSTYLE_PROP_Terminator_Width_DOUBLE);
		dimStylePtr->SetDoubleProp(scale / 2, DimStyleProp::DIMSTYLE_PROP_Terminator_Height_DOUBLE);

		// get rotationsmatrice for label
		RotMatrix rmLabel = RotMatrix::FromIdentity();
		rmLabel = rmLabel.FromAxisAndRotationAngle(2, rotAngleInDregrees * fc_piover180);
		LabelCellHeaderHandler::StdDPointVector vectorPt;
		vectorPt.push_back(ptTemp);
		vectorPt.push_back(ptOrigin);

		// create label and update reference
		LabelCellHeaderHandler::GetInstance().CreateLabel(labelElm, leaderElm, *dimStylePtr, ehCell, true, rmLabel, *ISessionMgr::GetActiveDgnModelRefP(), vectorPt, !placeLeader);
		LabelCellHeaderHandler::GetInstance().UpdateFieldTarget(labelElm, eeh);

		// restore terminator size
		dimStylePtr->SetDoubleProp(arrowSizeSave, DimStyleProp::DIMSTYLE_PROP_Terminator_Width_DOUBLE);
		dimStylePtr->SetDoubleProp(arrowSizeSave, DimStyleProp::DIMSTYLE_PROP_Terminator_Height_DOUBLE);


		// set grafic group number
		UInt32 ggNum = tcb->graphic;
		ElementPropertiesSetterPtr propSetter = ElementPropertiesSetter::Create();
		propSetter->SetGraphicGroup(labelElm, ggNum);
		propSetter->SetGraphicGroup(leaderElm, ggNum);
		eeh.ReplaceInModel(elmRefIn);
		mdlSystem_updateGraphicGroup();

		// add element(s) to file
		labelElm.AddToModel();
		if (placeLeader)
			leaderElm.AddToModel();

		mdlElmdscr_freeAll(&pedCell);
	}
	else
	{
		wchar_t sMsg[120];
		swprintf(sMsg, L"Error: Unable to find the cell <%hs>", cellName);
		mdlDialog_dmsgsPrint(sMsg);
		rc = ERROR;
	}

	return rc;
}

Below you'll find one way to call this function:

#define SCALE mdlModelRef_getUorPerMaster(mdlModelRef_getActive())
	virtual StatusInt _DoOperationForModify(EditElementHandleR eeh) override
	{
		DPoint3d cellOrigin;
		NormalCellHeaderHandler::GetInstance().GetSnapOrigin(eeh, cellOrigin);
		cellOrigin.x += 1.1*SCALE;
		cellOrigin.y += 1.0*SCALE;
		lifalib_placeLabel(eeh.GetElementRef(), "LandInd_alle", 0.1, cellOrigin, 1.1*SCALE, 1.0*SCALE, true, 0.0, eeh.GetElementRef()->GetLevel());

		return SUCCESS;
	}

It seems to work fine - a label is added to the design file together with a leader. But when I move the label-element the leader remains unchanged - it is moved together with the label-element, but I want the leader and terminator to be modified in the same way as if it was placed manually. In this case the teminator (the arrow) still points to the element the data is extracted from.

3386.CreateLabelTest.zip

I've attached a .zip file that includes Visual Studio project (CreateLabelTest), a design file used testing (CreateLabelTest.dgn), cellibrary (CreateLabelTest.cel) and LabelHandler.h (with small necessary changes if you want to compile the solution).

I indeed hope that someone can help me so that the labels created by the program "works" the same way as if it was placed manually..

TIA

Evan

Parents Reply Children
No Data