Load XREF from SQL

< previous    1 2 3    next >
Wout
3/18/2020 1:34 PM

Can you show the method you created to implement the delegate? The method is called by CadLib for each block that needs the external reference to be loaded when you call method

C# Code:
        DxfModel.LoadExternalReferences(
            DxfBlock.GetExternalReferenceDelegate customResolveExternalReference, 
            out IList<DxfBlock> missingReferences
        )

So for each block, it will call your own method 1 time, and it will pass the block as parameter, from which you can find out what to load (through DxfBlock.ExternalReference obviously).

- Wout

Lagen
3/18/2020 2:04 PM

Hi Wout

I try my best to understand this.
this code:

Visual Basic Code:
Dim missingReferences As IList(Of DxfBlock) = Nothing
MyDxfModel.LoadExternalReferences(DxfBlock.GetExternalReferenceDelegate, customResolveExternalReference, missingReferences)
give me error:
Delegate Function WW.Cad.ModeI.TabIes.Dxf810ck.GetExternaIReferenceDeIegate(bIockAs WW.Cad.ModeI.TabIes.Dxf810ck) As WW.Cad.ModeI.DxfModeI
Gets the referenced model for the specified block.
'Dxf810ck.GetExternaIReferenceDeIegate' is a type and cannot be used as an expression.

Visual Basic Code:
Public Delegate Function GetExternalReferenceDelegate(block As DxfBlock) As DxfModel

Wout
3/18/2020 2:20 PM

Hi Patrik,

Can you order 1 support ticket please?

The issue seems to be that you lack understanding of how to work with delegates in .NET. So please go over the VB documentation like for instance this web page by microsoft. I suggest you work through the example on this web page and understand how it works. The DxfModel.LoadExternalReferences call works in exactly the same way.

- Wout

Lagen
3/18/2020 2:42 PM

Hi Wout

I order 2 support ticket.

Wout
3/18/2020 2:55 PM

Hi Patrik,

Thank you.

With the Microsoft documentation I pointed you to you should at least be able to write some code that compiles. Once you got that far, let me know.

- Wout

Lagen
3/18/2020 3:06 PM

Hi Wout

I will do.


/Patrik

Lagen
3/18/2020 7:57 PM

Hi Wout

I don't know the next step, or am I total wrong?
I get my XREF in missingReferences, so far so good.

Visual Basic Code:
Public Class Form2
    Public Sub CustomLoadExternalReferences(ByVal model As DxfModel)
        Dim missingReferences As IList(Of DxfBlock) = Nothing
        model.LoadExternalReferences(AddressOf ProcessBlock, missingReferences)
        For i = 0 To missingReferences.Count - 1
            MsgBox(missingReferences.Item(i).ToString)
        Next
    End Sub

    Public Shared Function ProcessBlock(ByVal block As WW.Cad.Model.Tables.DxfBlock) As WW.Cad.Model.DxfModel
        Dim [return] As WW.Cad.Model.DxfModel = Nothing
        Return [return]
    End Function

    Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Using cn As SqlConnection = GetConnectionSQL()
            cn.Open()
            Using cmd As SqlCommand = New SqlCommand("select FileStream from DWG_FILESTREAM where FileName='my.dwg'", cn)
                Using dr As SqlDataReader = cmd.ExecuteReader(System.Data.CommandBehavior.[Default])
                    If dr.Read() Then
                        'Dim fileData As Byte() = CType(dr.GetValue(0), Byte())
                        Dim memoryStream As MemoryStream = New MemoryStream()
                        memoryStream = New MemoryStream(CType(dr.GetValue(0), Byte()))
                        Dim reader As DwgReader = New DwgReader(memoryStream)
                        reader.LoadUnknownObjects = True
                        Dim openModel As DxfModel = reader.Read
                        Dim displayedLayout As DxfLayout = If(openModel.Header.ShowModelSpace, openModel.ModelLayout, openModel.ActiveLayout)
                        Dim cadAnnotationControl As CadAnnotationControl = New CadAnnotationControl()
                        Dim annotationsModel As DxfModel = New DxfModel()
                        CustomLoadExternalReferences(openModel)
                    End If
                    dr.Close()
                End Using
            End Using
        End Using
    End Sub
    Private Function GetConnectionSQL() As SqlConnection
        Return New SqlConnection("Data Source =.\SQLExpress; Initial Catalog =TESTDB; Integrated Security =SSPI;")
    End Function
End Class

Wout
3/18/2020 8:16 PM

Hi Patrik,

This is close. Now you need your function ProcessBlock to load the DxfModel that is associated with the block it receives.

Just a side note: the way you construct your SQL statement on the fly is potentially a security hole. If you substitute "my.dwg" with some user input string, and the user knows how to write SQL, then you're open to an SQL injection attack.

- Wout

Lagen
3/18/2020 8:47 PM

I guess I have to load the model like this, but how I load that in ProcessBlock I don't know.


Visual Basic Code:
Function openXREF_DWG(ByVal DWG_Name As String) As DxfModel
        Using cn As SqlConnection = GetConnectionSQL()
            cn.Open()
            Using cmd As SqlCommand = New SqlCommand("select FileStream from DWG_FILESTREAM where FileName='" & DWG_Name & ".DWG'", cn)
                Using dr As SqlDataReader = cmd.ExecuteReader(System.Data.CommandBehavior.[Default])
                    If dr.Read() Then
                        'Dim fileData As Byte() = CType(dr.GetValue(0), Byte())
                        Dim memoryStream As MemoryStream = New MemoryStream()
                        memoryStream = New MemoryStream(CType(dr.GetValue(0), Byte()))
                        Dim reader As DwgReader = New DwgReader(memoryStream)
                        reader.LoadUnknownObjects = True
                        Dim Model As DxfModel = reader.Read
                        Return Model
                    End If
                    dr.Close()
                End Using
            End Using
        End Using
    End Function

Lagen
3/18/2020 9:04 PM

I guess something like this
in ProcessBlock I only see the first XREF.
but in missingReferences.Item I see all my XREF
but this row give error: cadAnnotationControl.Model.LoadExternalReferences(openDWG(missingReferences.Item(i).ToString))

Visual Basic Code:
Imports WW.Cad.Model
Imports WW.Cad.Model.Tables
Imports System.Data.SqlClient
Imports System.IO

Imports WW.Cad.IO
Imports WW.Cad.Model.Objects
Imports WW.Cad.UI.Extensions
Imports WW.Cad.UI.Windows.Forms
Imports WeifenLuo.WinFormsUI.Docking


Public Class Form2
    Public Function CustomLoadExternalReferences(ByVal model As DxfModel) As DxfModel
        Dim missingReferences As IList(Of DxfBlock) = Nothing
        model.LoadExternalReferences(AddressOf ProcessBlock, missingReferences)
        Dim cadEditControl As WW.Cad.UI.Windows.Forms.CadEditControl = Me.ActiveCadEditControl
        Dim cadAnnotationControl As WW.Cad.UI.Extensions.CadAnnotationControl = TryCast(cadEditControl, WW.Cad.UI.Extensions.CadAnnotationControl)
        For i = 0 To missingReferences.Count - 1
            'Add multi XREF files
            cadAnnotationControl.Model.LoadExternalReferences(openDWG(missingReferences.Item(i).ToString))
            '
        Next
    End Function


    Public Shared Function ProcessBlock(ByVal block As WW.Cad.Model.Tables.DxfBlock) As WW.Cad.Model.DxfModel
        Dim [return] As WW.Cad.Model.DxfModel = Nothing
        Return [return]
    End Function
    Function openDWG(ByVal DWG_Name As String) As DxfModel
        Using cn As SqlConnection = GetConnectionSQL()
            cn.Open()
            Using cmd As SqlCommand = New SqlCommand("select FileStream from DWG_FILESTREAM where FileName='" & DWG_Name & ".DWG'", cn)
                Using dr As SqlDataReader = cmd.ExecuteReader(System.Data.CommandBehavior.[Default])
                    If dr.Read() Then
                        'Dim fileData As Byte() = CType(dr.GetValue(0), Byte())
                        Dim memoryStream As MemoryStream = New MemoryStream()
                        memoryStream = New MemoryStream(CType(dr.GetValue(0), Byte()))
                        Dim reader As DwgReader = New DwgReader(memoryStream)
                        reader.LoadUnknownObjects = True
                        Dim Model As DxfModel = reader.Read
                        Return Model
                    End If
                    dr.Close()
                End Using
            End Using
        End Using
    End Function

    Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Using cn As SqlConnection = GetConnectionSQL()
            cn.Open()
            Using cmd As SqlCommand = New SqlCommand("select FileStream from DWG_FILESTREAM where FileName='E64F_0130102M.dwg'", cn)
                Using dr As SqlDataReader = cmd.ExecuteReader(System.Data.CommandBehavior.[Default])
                    If dr.Read() Then
                        'Dim fileData As Byte() = CType(dr.GetValue(0), Byte())
                        Dim memoryStream As MemoryStream = New MemoryStream()
                        memoryStream = New MemoryStream(CType(dr.GetValue(0), Byte()))
                        Dim reader As DwgReader = New DwgReader(memoryStream)
                        reader.LoadUnknownObjects = True
                        Dim openModel As DxfModel = reader.Read
                        Dim displayedLayout As DxfLayout = If(openModel.Header.ShowModelSpace, openModel.ModelLayout, openModel.ActiveLayout)
                        Dim cadAnnotationControl As CadAnnotationControl = New CadAnnotationControl()
                        Dim annotationsModel As DxfModel = New DxfModel()
                        CustomLoadExternalReferences(openModel)
                    End If
                    dr.Close()
                End Using
            End Using
        End Using
    End Sub
    Private Function GetConnectionSQL() As SqlConnection
        Return New SqlConnection("Data Source =.\SQLExpress; Initial Catalog =LOCUM_TEST; Integrated Security =SSPI;")
    End Function
    Public ReadOnly Property ActiveCadEditControl As CadEditControl
        Get
            Dim item As CadEditControl
            If (Me.dockPanel.ActiveDocument Is Nothing) Then
                item = Nothing
            Else
                item = TryCast(DirectCast(Me.dockPanel.ActiveDocument, DockContent).Controls(0), CadEditControl)
            End If
            Return item
        End Get
    End Property
End Class

< previous    1 2 3    next >