70002567 How to insert a specific block from an other existing drawing

Article 70002567
Type HowTo
Product Engine
Version 1101
Date Added 9/20/2024 12:00:00 AM
Fixed 11.2.4.0 (9/20/2024 12:00:00 AM)
Submitted by VectorDraw Team

Summary

How to insert a specific block from an other existing drawing

Solution

c# code:

               //suppose blocklibrary.dwg exist and contains a block with Name 'myblock'
                String sourceFilePath = @"c:\blocklibrary.dwg";
                String blockName = "myblock";
                double xInsert = 10.0, yInsert = 10.0, zInsert = 0.0;

                VectorDraw.Professional.Components.vdDocumentComponent tmpdoccomponet = new VectorDraw.Professional.Components.vdDocumentComponent();


                vdDocument tmpdoc = tmpdoccomponet.Document; //get a temporary document
                tmpdoc.Open(sourceFilePath);//open the drawing that contains the block

                //get the block object and export as vdDocument memory object
                vdBlock blk = tmpdoc.Blocks.FindName(blockName);
                vdDocument blockdoc = blk.ToDocument();

                //insert the previous vddocument memory reference the block to the blocks of the original document
                vdBlock block_new = doc.Blocks.AddFromDocument(blockName, blockdoc, false);

                //create and add an insert object reference the new inserted block
                vdInsert ins = new vdInsert(doc);
                ins.InsertionPoint = new gPoint(xInsert, yInsert, zInsert);
                ins.Block = block_new;
                doc.Model.Entities.AddItem(ins);


                doc.ActiveLayOut.ZoomExtents();
                doc.Redraw(true);

c++ ActiveX

        #import "System.Drawing.tlb"
        #import "mscorlib.tlb" rename("ReportEvent","_ReportEvent")
        #import "VectorDraw.Serialize.tlb"
        #import "VectorDraw.Geometry.tlb" 
        #import "VectorDraw.Render.tlb"         
        #import "VectorDraw.Actions.tlb"
        #import "VectorDraw.Professional.tlb"  rename("GetEnvironmentVariable","_GetEnvironmentVariable")  
        #import "vdrawi5.tlb"    \
	rename("RGB","_RGB")  \
	rename("GetOpenFileName","_GetOpenFileName")  \
              rename("GetEnvironmentVariable","_GetEnvironmentVariable")  \


                            //suppose blocklibrary.dwg exist and contains a block with Name 'myblock'
		CString sourceFilePath = _T("D:\\blocklibrary.dwg");
		CString blockName = _T("myblock");
		double xInsert = 10.0, yInsert = 10.0, zInsert = 0.0;

		CVdraw tmpvd;//a temporary vdraw control to hold a new document it will be disposed on CVdraw deconstructor on function exit
		tmpvd.Create(_T(""), WS_CHILD, CRect(0, 0, 0, 0), this, -1);
		tmpvd.ShowWindow(SW_HIDE);

		
		VectorDraw_Professional::IvdDocumentPtr tmpdoc = tmpvd.GetActiveDocument().GetWrapperObject();//get the document of the temporary vdraw control
		tmpdoc->Open(bstr_t(sourceFilePath));//open the drawing that contains the block
		
		//get the block object and export as vdDocument memory object
		VectorDraw_Professional::IvdBlockPtr blk = tmpdoc->Blocks->FindName(_bstr_t(blockName));
		VectorDraw_Professional::IvdDocumentPtr blockdoc = blk->ToDocument();

		//insert the previous vddocument memory reference the block to the blocks of the original document
		VectorDraw_Professional::IvdDocumentPtr doc = mVdraw.GetActiveDocument().GetWrapperObject();
		VectorDraw_Professional::IvdBlockPtr block_new = doc->Blocks->AddFromDocument(_bstr_t(blockName), blockdoc, VARIANT_FALSE);

		//create and add an insert object reference the new inserted block
		VectorDraw_Professional::IvdInsertPtr ins = doc->Activator->CreateInstance("vdInsert");
		((VectorDraw_Professional::IvdBaseObjectPtr)ins)->SetUnRegisterDocument(doc);
		((VectorDraw_Professional::IvdPrimaryPtr)ins)->setDocumentDefaults();
		ins->insertionPoint->SetValue(xInsert, yInsert, zInsert);
		ins->Block = block_new;
		doc->Model->Entities->AddItem((VectorDraw_Professional::IvdFigurePtr)ins);


		doc->ActiveLayOut->ZoomExtents();
		doc->Redraw(VARIANT_TRUE);

Send comments on this topic.