Sparx Systems Forum

Enterprise Architect => Automation Interface, Add-Ins and Tools => Topic started by: MichPaule on April 22, 2024, 04:19:37 pm

Title: Document created programmatically ends up as R/O
Post by: MichPaule on April 22, 2024, 04:19:37 pm
I succeeded creating Document elements containing externally provided RTF.
Code: [Select]
Dim art As EA.Element : Set art = parent.Elements.AddNew(nam, "Artifact")
art.Update
art.StereotypeEx = "Document"
art.Version = "1.0"
art.Update
' Add document content
Dim xid As String : xid = CreateGUID()
Dim docname As String : docname = parent.Name & "::" & art.Name
Dim bin As String : bin = PackFile(rtf_file)
Call Repository.Execute("INSERT INTO `t_document` (DocID, DocName, Style, ElementID, ElementType, BinContent, DocType, IsActive, Sequence, DocDate) VALUES ('" & xid & "', '" & docname & "', '', '" & art.ElementGUID & "', 'ModelDocument', X'" & bin & "', 'ModelDocument', '0', '0', '" & Format(Now(), "yyyy-MM-dd hh:mm:ss") & "');")


Function PackFile(ByVal fname As String) As String
    If (m_fso.FileExists(fname)) Then
        ' Copy file to new "%temp%\str.dat"
        Dim dstname
        dstname = m_fso.BuildPath(m_fso.GetSpecialFolder(2).Path, "str.dat")
        Call m_fso.CopyFile(fname, dstname)
        Dim zipname
        zipname = m_fso.BuildPath(m_fso.GetSpecialFolder(2).Path, "str.zip")
        With m_fso.OpenTextFile(zipname, ForWriting, True)
            ' this is the header to designate a file as a zip
            .Write "PK" & Chr(5) & Chr(6) & String(18, Chr(0))
            .Close
        End With
        With CreateObject("Shell.Application")
            Dim dns: Set dns = .Namespace(zipname)
            Call dns.CopyHere(dstname, 1024 + 16 + 4)
            Dim waitTill: waitTill = Now() + TimeValue("00:00:01")
            While Now() < waitTill
                DoEvents
            Wend
        End With
        With CreateObject("ADODB.Stream")
            .Type = 1  'adTypeBinary
            .Open
            Call .LoadFromFile(zipname)
            Dim node: Set node = CreateObject("MSXML2.DOMDocument").createElement("bin")
            node.DataType = "bin.hex"
            node.nodeTypedValue = .Read()
            PackFile = node.Text
        End With
    End If
End Function
The RTF is rendered fine (as far as I can say).
But EA doesn't allow changing the content of these Document files.
I'm unable to find the reason for this behavior.
Maybe someone already has come across this problem and could provide some insight?
TIA

Michael
Title: Re: Document created programmatically ends up as R/O
Post by: MichPaule on January 24, 2025, 12:06:22 am
Ok, after a long time I had to come back to this topic...

It looks like the documents end up in state "Text Selection Lock" = enabled (see Edit > Manage > Text Selection Lock).
Selecting all contents and disabling "Text Selection Lock" is the first step.
Then the "Protect" flag (see Edit > Font > Protect) which was greyed out before will become activated.
Again select all contents and disable "Protect" to make the document contents editable.

For now I don't care about changing these settings programmatically but if anyone has ideas I would highly appreciate it!  :)
Title: Re: Document created programmatically ends up as R/O
Post by: Geert Bellekens on January 24, 2025, 01:09:28 am
Have you tried using Element.LoadLinkedDocument(string Filename)
I think that does the same as your code.

Geert
Title: Re: Document created programmatically ends up as R/O
Post by: MichPaule on January 25, 2025, 03:04:35 am
Have you tried using Element.LoadLinkedDocument(string Filename)
I think that does the same as your code.

Geert
Hi Geert,
as always you are right! ;D
It is working perfectly fine for me - and there is no need to unlock anything after creation.
A single call replaces my lengthy and troublesome code.
Wow, I don't get it why I have overlooked this call...
Thanks a lot!

Michael