Is it possible to add and save a certain GroupBlock to a library without using CreateItemDlg method from the TDgrLibraryManager class?
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;
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"
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;