Quantcast
Channel: MicroStation Programming Forum - Recent Threads
Viewing all articles
Browse latest Browse all 4331

Scale all elements in model by 25.4

$
0
0

I'm using Microstation Connect Update 10 and I'm trying to modify a dgn file(not the active file) and then save it as a dwg file for our cnc operations. Part of the modifications include dropping all the cells and complex elements to geometry, deleting some object and modifying others (rotate, move, etc...), and finally scaling up the entire drawing by 25.4 as it needs to be metric for this particular machine to read. I'm attaching the code I have so far which works perfectly for the active file if I change the model ref. It also works perfectly for converting an unopened dgn file to a dwg.... accept for the scaling that takes place at the end. When I run this it does everything it is supposed to, but when I include the scaling portion it makes a complete mess of the active drawing even though I don't have it referenced anywhere in code. 

Any Help Appreciated

Thanks

Dave

void SaveAsDwg (WCharCP unparsed)
{
	/* create a pointer and point at nothing */
	DgnFilePtr dgnfileStnHr = nullptr;

	/* original file path and name */
	WString fileName(LR"(C:\Users\drodenhizer\Documents\2022-172 TSCP-1.dgn)");

	/* document pointer to above file */
	DgnDocumentPtr dgndocStnhrSeed = DgnDocument::CreateForLocalFile(fileName.c_str());
	
	/* create a set to store levels to be modified */
	set<UInt32> intLstLvls{ };
	MSElementDescr* eledscr = 0;
	UInt32 filePos = 0;
	/* check to see if pointer is valid	*/
	if (dgndocStnhrSeed.IsValid())
	{
		// create an in memory representation of original file
		dgnfileStnHr = DgnFile::Create(*dgndocStnhrSeed, DgnFileOpenMode::ReadWrite);

		// check file validity
		if (dgnfileStnHr.IsValid())
		{	
			// needed for dgndocument return values
		   	StatusInt *stsLoad = new StatusInt();
			DgnFileStatus newfile = DgnFileStatus();
			
			// load file
			dgnfileStnHr->LoadDgnFile(stsLoad);

			// complete name for new file
			WString fileNameNew(LR"(C:\Users\drode\Documents\2022-XXX TSCP-1.dwg)");

			// location for new file
			WString filePathNew(LR"(C:\Users\drode\Documents\)");

			// create a new document pointer for the save as function to overwrite
			DgnDocumentPtr dgndocStnhr = DgnDocument::CreateForNewFile(newfile, fileNameNew.c_str(),
				NULL, (int)DgnFileFormatType::Current, filePathNew.c_str(), DgnDocument::OverwriteMode::Always, DgnDocument::CreateOptions::Default);

			// save original file under new file name. note: even though this is the new file, manipulations are still preformed on the original
			dgnfileStnHr->DoSaveAs(*dgndocStnhr, DgnFileFormatType::DWG, true, false);
			Int32 mdlId = dgnfileStnHr->GetDefaultModelId();

			DgnModelP model = dgnfileStnHr->FindLoadedModelById(mdlId);			
			Int32 eleCnt = model->GetElementCount(DgnModelSections::All);
			WPrintfString myStr(L"");
			myStr.Sprintf(L"ele Count = %zu,", eleCnt);
			mdlDialog_dmsgsPrint(myStr.GetWCharCP());
			dgnfileStnHr->FillSectionsInModel(model, DgnModelSections::All);

			GetLevelIds(intLstLvls, *model);
				
				/* loop through all elements drop all cells to geometry	*/
				for (EditElementHandle eehSteinElms : model->GetElementsCollection())
				{
					// ================ this works ========================================================↓
					if (CELL_HEADER_ELM == eehSteinElms.GetElementType())
					{		
						DisplayHandlerP handler = eehSteinElms.GetDisplayHandler();
					
						if (nullptr != handler)
						{
							NormalCellHeaderHandler* cellHandler = dynamic_cast<NormalCellHeaderHandler*>(handler);
							if (nullptr != cellHandler)
							{
								DropGeometryPtr dropGeometry = DropGeometry::Create();
								dropGeometry->SetOptions(DropGeometry::OPTION_Complex);
								ElementAgenda agenda;
								WChar name[MAX_MODEL_NAME_LENGTH];
								cellHandler->ExtractName(name, MAX_MODEL_NAME_LENGTH, eehSteinElms);
								cellHandler->Drop(eehSteinElms, agenda, *dropGeometry);
								for (EditElementHandleR eehR : agenda)
								{
									eehR.AddToModel();
								}
							}
						}
						eehSteinElms.DeleteFromModel();
					}
				}
				/* loops through all elements and scales up by 25.4	*/
				for (EditElementHandle ehForScl : model->GetElementsCollection())
				{
					if (SUCCESS == mdlAssoc_getElementDescr(&eledscr, &filePos, ehForScl.GetElementId(),ehForScl.GetModelRef() , FALSE))
					{
						Transform tMatrix;
						mdlTMatrix_getIdentity(&tMatrix);
						Transform sMatrix;
						mdlTMatrix_getIdentity(&sMatrix);
						mdlTMatrix_scale(&sMatrix, &sMatrix, 25.4, 25.4, 25.4);
						mdlElmdscr_transformAllowModification(&eledscr, &tMatrix, ehForScl.GetModelRef(), ehForScl.GetModelRef(), 0);
						mdlElmdscr_transform(&eledscr, &sMatrix);
						mdlElmdscr_rewrite(eledscr, eledscr, filePos);
						mdlElmdscr_freeAll(&eledscr);
					}
				}

			dgnfileStnHr->ProcessChanges(DgnSaveReason::FileSaveAs);
		}
	}
}

Viewing all articles
Browse latest Browse all 4331


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>