Insert complete block (object) on position

Discuss and ask questions about CAD .NET library.

Moderators: SDS, support, admin

Post Reply
mike32m
Posts: 13
Joined: 17 Jun 2008, 16:06

Insert complete block (object) on position

Post by mike32m » 26 Jun 2008, 18:57

Hello,
we must add two seperate blocks or objects from dxf-files in one new cad-image. Can help someone and give us some helpful ideas ?
Greatings Mike

support
Posts: 3272
Joined: 30 Mar 2005, 11:36
Contact:

Re: Insert complete block (object) on position

Post by support » 30 Jun 2008, 12:53

Hello Mike,

It is necessary to do the following:
  1. to find necessary block in a source file
  2. to get all its entities
  3. to create new block in a destination file
  4. to create new entities and add them to a new block in a destination file
  5. new entities must be equal to entities from the block of a source file
or try the following code:

Code: Select all

		private void btnAssignBlocks_Click(object sender, System.EventArgs e)
		{
			this.cadImage = new CADImage();				
			this.cadImage.UseDoubleBuffering = false;
			this.cadImage.InitialNewImage();
			SetLayList();					

			this.cadImage.LoadFromFile(@"c:\FileBlocksToBeInsertedIn.dxf");
			SetCADImageOptions();
			string fName = @"c:\FileBlocksToBeTakenFrom.dxf";
			CADImage cadImageSource = CADImage.CreateImageByExtension(fName);
			cadImageSource.LoadFromFile(fName);
			for (int i=0;i<cadImageSource.CurrentLayout.Converter.GetCounts(CADImport.FaceModule.ConvSection.Entities);i++)
			{				
				CADEntity entSource = cadImageSource.CurrentLayout.Converter.GetSection(CADImport.FaceModule.ConvSection.Entities).Entities[i];
				if (entSource is CADInsert)
				{					
					CADInsert entInsert = entSource as CADInsert;
					CADBlock bl1 = new CADBlock();
					bl1.Name = entInsert.Block.Name;
					bl1.Color = entInsert.Block.Color;
					bl1.Visibility = true;
					bl1.Visible = true;					

					for (int j=0;j<entInsert.Block.Count;j++)
					{
						CADEntity entInBlock = entInsert.Block.Entities[j];						
						Type tp = entInBlock.GetType();
						CADEntity entity = (CADEntity)Activator.CreateInstance(tp);						
						entity.AssignEntity(entInBlock);
						entity.Loaded(this.cadImage.Converter);
						this.cadImage.Converter.OnCreate(entity);	
						bl1.AddEntity(entity);
					}

					this.cadImage.Converter.Blocks.Add(bl1);
					this.cadImage.Converter.OnCreate(bl1);
					this.cadImage.Converter.Loads(bl1);

					CADInsert ins1 = new CADInsert();
					ins1.Block = bl1;
					ins1.Point = entInsert.Point;
					ins1.Visibility = true;
					this.cadImage.Converter.Entities.Add(ins1);
					this.cadImage.Converter.OnCreate(ins1);
					this.cadImage.Converter.Loads(ins1);
				}				
			}
			this.cadPictBox.Invalidate();

			CADtoDXF exportImage = new CADtoDXF(cadImage);
			exportImage.SaveToFile(@"c:\output.dxf");
		}
Sergey.
Technical Support E-mail: support@cadsofttools.com
Chat support on Skype: cadsofttools.support

mike32m
Posts: 13
Joined: 17 Jun 2008, 16:06

Re: Insert complete block (object) on position

Post by mike32m » 03 Jul 2008, 18:47

Thanks it's fine, but this is not the whole result I need.
Now I use the AddScaledDXF-Method für insert a complete DXF-File. So I don't think about the entitys of the unknown insert-File.
After insert I have an insert-entity with my own name. I can found it simple in the whole datas. That's ok.
But the next problem is: The rotation of the inserted object. I can placed it on needed position but only in rotation of one axis (i think it was the x-axis). I need a transformation of the object in x,y,z-position and in rotation from x,y,z-axis.
How can i do this ?

support
Posts: 3272
Joined: 30 Mar 2005, 11:36
Contact:

Re: Insert complete block (object) on position

Post by support » 08 Jul 2008, 14:30

Hello!
Function AddScaledDXF returns TsgDXFInsert object. We recommend to use the following code:

Code: Select all

var
...
  vIns: TsgDXFInsert;
begin
...
   vIns := TsgDXFInsert(TsgDXFImage.AddScaledDXFEx(...);

   vIns.Extrusion := MakeFPoint(0.577, 0.577, 0.577);// Insert contains Block with XREF
   TsgDXFImage.Converter.Loads(vIns);
   TsgDXFImage.GetExtents;
...
Sergey.
Technical Support E-mail: support@cadsofttools.com
Chat support on Skype: cadsofttools.support

mike32m
Posts: 13
Joined: 17 Jun 2008, 16:06

Re: Insert complete block (object) on position

Post by mike32m » 15 Jul 2008, 14:56

What class is TsgDXFInsert ??
I can't find it.

support
Posts: 3272
Joined: 30 Mar 2005, 11:36
Contact:

Re: Insert complete block (object) on position

Post by support » 15 Jul 2008, 18:16

Sorry, we gave you code for Delphi. Please try the following (it is based on Viewer demo):

Code: Select all

private void btnAddScaledDXF_Extrusion_Click(object sender, System.EventArgs e)
{
	CADInsert entInsert;
	if(this.cadImage == null) return;

	this.cadImage.UseDoubleBuffering = false;
	CADImage aFile = new CADImage();
	aFile.LoadFromFile(@"c:\Test.dxf");
	this.cadImage.AddScaledDXF(aFile, "c:\\Test.dxf", new DPoint(200,0,0), new DPoint(1,1,1), 0);
	for (int i=0;i<this.cadImage.Converter.GetCounts(CADImport.ConvSection.Entities);i++)
	{
		if (this.cadImage.Converter.GetSection(CADImport.ConvSection.Entities).Entities[i] is CADInsert)
		{
			entInsert = (CADInsert)this.cadImage.Converter.GetSection(CADImport.ConvSection.Entities).Entities[i];
			if (entInsert.Block.Name == "c:\\Test.dxf")
			{
				CADImport.DPoint nPoint = new DPoint(0.577, 0.577, 0.577);
				entInsert.Extrusion = new DPoint(0.577, 0.577, 0.577);
				this.cadImage.Converter.Loads(entInsert);			
			}
		}
	}
	this.DoResize();	
}
Sergey.
Technical Support E-mail: support@cadsofttools.com
Chat support on Skype: cadsofttools.support

easylift
Posts: 3
Joined: 19 Mar 2009, 13:28

Re: Insert complete block (object) on position

Post by easylift » 19 Mar 2009, 14:05

I converted in vb.net the first example, but I have an error on:
Dim entInsert As CADInsert = entSource
entSource in c CADLine type, the error message is: "Cast error, cannot convert CADLine to CADInsert"

somebody know how to insert a dwg block into a dwg file?
thanks

here is it all the code:
Me.cadImage = New CADImage()
Me.cadImage.UseDoubleBuffering = False
Me.cadImage.InitialNewImage()
SetLayList()

Me.cadImage.LoadFromFile("c:\file1.dwg")
SetCADImageOptions()
Dim fName As String = "c:\block1.dwg"
Dim cadImageSource As CADImage = cadImage.CreateImageByExtension(fName)
cadImageSource.LoadFromFile(fName)
Dim i As Integer

For i = 0 To cadImageSource.CurrentLayout.Converter.GetCounts(CADImport.FaceModule.ConvSection.Entities)
Dim entSource As CADEntity = cadImageSource.CurrentLayout.Converter.GetSection(CADImport.FaceModule.ConvSection.Entities).Entities.Item(i)
'If entSource Is CADInsert Then '<-does'nt work in vb.net

'Dim entInsert As CADInsert = entSource
Dim entInsert As CADInsert = entSource
Dim bl1 As New CADBlock()
bl1.Name = entInsert.Block.Name
bl1.Color = entInsert.Block.Color
bl1.Visibility = True
bl1.Visible = True

Dim j As Integer
For j = 0 To entInsert.Block.Count
Dim entInBlock As CADEntity = entInsert.Block.Entities(j)
Dim tp As Type = entInBlock.GetType()
Dim entity As CADEntity = Activator.CreateInstance(tp)
entity.AssignEntity(entInBlock)
entity.Loaded(Me.cadImage.Converter)
Me.cadImage.Converter.OnCreate(entity)
bl1.AddEntity(entity)
Next j

Me.cadImage.Converter.Blocks.Add(bl1)
Me.cadImage.Converter.OnCreate(bl1)
Me.cadImage.Converter.Loads(bl1)

Dim ins1 As New CADInsert()
ins1.Block = bl1
ins1.Point = entInsert.Point
ins1.Visibility = True
Me.cadImage.Converter.Entities.Add(ins1)
Me.cadImage.Converter.OnCreate(ins1)
Me.cadImage.Converter.Loads(ins1)

'End If 'If entSource Is CADInsert Then
Next i
Me.cadPictBox.Invalidate()

Dim exportImage As New CADtoDXF(cadImage)
exportImage.SaveToFile("c:\temp\output.dxf")

support
Posts: 3272
Joined: 30 Mar 2005, 11:36
Contact:

Re: Insert complete block (object) on position

Post by support » 19 Mar 2009, 15:32

Hello!

Please try the followin code:

Code: Select all

    Private Sub btnAssignBlocks_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAssignBlocks.Click
        Dim I, J As Integer

        Me.cadImage = New CADImage
        Me.cadImage.UseDoubleBuffering = False
        Me.cadImage.InitialNewImage()
        SetLayList()

        Me.cadImage.LoadFromFile("c:\FileBlocksToBeInsertedIn.dxf")
        SetCADImageOptions()

        Dim fName As String = "c:\FileBlocksToBeTakenFrom.dxf"

        Dim cadImageSource As CADImage = cadImage.CreateImageByExtension(fName)
        cadImageSource.LoadFromFile(fName)
        
        For I = 0 To cadImageSource.CurrentLayout.Converter.GetCounts(CADImport.FaceModule.ConvSection.Entities) - 1
            Dim entSource As CADEntity = cadImageSource.CurrentLayout.Converter.GetSection(CADImport.FaceModule.ConvSection.Entities).Entities(I)
            If (TypeOf entSource Is CADInsert) Then

                Dim entInsert As CADInsert = entSource
                Dim bl1 As CADBlock = New CADBlock
                bl1.Name = entInsert.Block.Name
                bl1.Color = entInsert.Block.Color
                bl1.Visibility = True
                bl1.Visible = True

                For J = 0 To entInsert.Block.Count - 1
                    Dim entInBlock As CADEntity = entInsert.Block.Entities(J)
                    Dim tp As Type = entInBlock.GetType()
                    Dim entity As CADEntity = Activator.CreateInstance(tp)

                    entity.AssignEntity(entInBlock)
                    entity.Loaded(Me.cadImage.Converter)
                    Me.cadImage.Converter.OnCreate(entity)
                    bl1.AddEntity(entity)
                Next J

                Me.cadImage.Converter.Blocks.Add(bl1)
                Me.cadImage.Converter.OnCreate(bl1)
                Me.cadImage.Converter.Loads(bl1)

                Dim ins1 As CADInsert = New CADInsert
                ins1.Block = bl1
                ins1.Point = entInsert.Point
                ins1.Visibility = True
                Me.cadImage.Converter.Entities.Add(ins1)
                Me.cadImage.Converter.OnCreate(ins1)
                Me.cadImage.Converter.Loads(ins1)
            End If
        Next I
        Me.cadPictBox.Invalidate()

        Dim exportImage As CADtoDXF = New CADtoDXF(cadImage)
        exportImage.SaveToFile("c:\output.dxf")
    End Sub
Sergey.
Technical Support E-mail: support@cadsofttools.com
Chat support on Skype: cadsofttools.support

Post Reply