Web forum is in read-only mode. Login as active registered customer for write access
  Forum Search   New Posts New Posts

Create/save TDgrLibraryItem programmatically

 Post Reply Post Reply
Author
Krauß Dr. Peter W. View Drop Down
New Member
New Member
Avatar

Joined: 09 May 2015
Posts: 2
Post Options Post Options   Quote Krauß Dr. Peter W. Quote  Post ReplyReply Direct Link To This Post Topic: Create/save TDgrLibraryItem programmatically
    Posted: 23 Apr 2019 at 2:17pm
Is it possible to add and save a certain GroupBlock to a library without using CreateItemDlg method from the TDgrLibraryManager class?
Back to Top
Wagner R. Landgraf View Drop Down
TMS Support
TMS Support
Avatar

Joined: 18 May 2010
Posts: 2365
Post Options Post Options   Quote Wagner R. Landgraf Quote  Post ReplyReply Direct Link To This Post Posted: 23 Apr 2019 at 10:46pm
Yes, you can extract the code from CreateItemDlg itself, by reducing it without the call to the visual editor. For example:

function CreateItem(LibManager: TDgrLibraryManager; ADControl: TDiagramControl; LibName: string): TDgrLibraryItem;
var
  AItem: TDgrLibraryItem;
  ALibrary: TDgrLibrary;
begin
    AItem := TDgrLibraryItem.Create(nil);
    try
      ALibrary := LibManager.LibraryByName(LibName);
      if ALibrary = nil then
        ALibrary := LibManager.CreateLibrary(LibName);
      AItem.Data := ADControl.Diagram.SerializeObjectToString(ADControl);

      ALibrary.AddItem(AItem);
      AItem.Save;
      result := AItem;
      result.RegisterItem;
    except
      AItem.Free;
      raise;
    end;
end;

Back to Top
Krauß Dr. Peter W. View Drop Down
New Member
New Member
Avatar

Joined: 09 May 2015
Posts: 2
Post Options Post Options   Quote Krauß Dr. Peter W. Quote  Post ReplyReply Direct Link To This Post Posted: 24 Apr 2019 at 10:19am
Thanks a lot for the quick answer. 
I just tried the code butI have a problem to access to following members:
1. "ADControl.Diagram.SerializeObjectToString(..)"
2. "ALibrary.AddItem(..)"
3. "result.RegisterItem"
Back to Top
Wagner R. Landgraf View Drop Down
TMS Support
TMS Support
Avatar

Joined: 18 May 2010
Posts: 2365
Post Options Post Options   Quote Wagner R. Landgraf Quote  Post ReplyReply Direct Link To This Post Posted: 30 Apr 2019 at 7:33pm
You can use this modified code, but indeed you will have to move SerializeObjectToString and RegisterItem methods to the public section in their classes. We have done it here and in next Diagram Studio release will have those methods in public section.

function CreateItem(LibManager: TDgrLibraryManager; ADControl: TDiagramControl; LibName: string): TDgrLibraryItem;
var
  AItem: TDgrLibraryItem;
  ALibrary: TDgrLibrary;
begin
  ALibrary := LibManager.LibraryByName(LibName);
  if ALibrary = nil then
    ALibrary := LibManager.CreateLibrary(LibName);
  AItem := ALibrary.NewItem;
  try
    AItem.Caption := 'New block 1';
    AItem.Data := ADControl.Diagram.SerializeObjectToString(ADControl);
    AItem.Save;
    result := AItem;
    result.RegisterItem;
  except
    AItem.Free;
    raise;
  end;
end;
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down