problem adding block with attribute

Marco Del Frate
4/19/2021 11:12 AM

Good morning,

i've just updated my program with from when i create a model adding a block with attribute and try to open it with autocad, it tell me that there are many error in the file. pease note that it's the same code and block that i used with the previous edition and that works well.
Attached You can find a little program that allow you to recreate my problem.
Please help me, thank You in advance
Best regards

4/23/2021 11:05 AM


Your cloning code was incorrect, it was defining the clone as from Model to Model, but it should be from ModelSorg (source model) to Model (target model). Below is how you should do the cloning:

Visual Basic Code:
Imports WW.Cad.Drawing
Imports WW.Cad.IO
Imports WW.Cad.Model
Imports WW.Cad.Model.Entities
Imports WW.Cad.Model.Tables
Imports WW.Math
Imports System.Drawing
Imports System.Drawing.Printing
Imports WW.Cad.Base
Imports WW.Cad.Drawing.GDI
Imports System.Environment

Class MainWindow
    Private Sub Bt_Test_Click(sender As Object, e As RoutedEventArgs)
        Dim Model As New DxfModel(DxfVersion.Dxf14)
        Dim StileSTQ As DxfTextStyle = New DxfTextStyle("STQ", "arial.ttf")

        If CB1.IsChecked Then AggiungiBlocco(Model, "A3_AI_PLUS.dwg", "A3_AI_PLUS")
        If CB2.IsChecked Then AggiungiBlocco(Model, "DatiPrin.dwg", "DatiPrin")

        Dim layer1 As New DxfLayer("A01")

        Dim Nfile As String = Environment.GetFolderPath(SpecialFolder.DesktopDirectory) & "\WW_Test2021.dwg"

        DwgWriter.Write(Nfile, Model)

        Dim boundsCalculator As New BoundsCalculator()
        Dim bounds As Bounds3D = boundsCalculator.Bounds
        Dim active As DxfVPort = New DxfVPort(DxfVPort.ActiveVPortName)
        active.Height = bounds.Delta.Y
        active.AspectRatio = bounds.Delta.X / bounds.Delta.Y
        active.Center = bounds.Center
        DwgWriter.Write(Nfile, Model)
        MsgBox("File created")
    End Sub

    Sub AggiungiBlocco(Model As DxfModel, NomeDis As String, NomeBlocco As String)
        Dim ModelSorg As DxfModel = WW.Cad.IO.DwgReader.Read(My.Application.Info.DirectoryPath & "\Resources\" & NomeDis)

        Dim cc As New CloneContext(ModelSorg, Model, ReferenceResolutionType.CloneMissing)

        For Each Ent In ModelSorg.Entities
            If Ent.EntityType <> "INSERT" Then Model.Entities.Add(Ent.Clone(cc))
        Dim srcBlock As DxfBlock = ModelSorg.GetBlockWithName(NomeBlocco)
        Dim targetBlock As DxfBlock = srcBlock.Clone(cc)

    End Sub
End Class

- Wout