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;