Probleme with MLeaderStyle

Hi,
I have a Probleme with the PostMLeaderStyleToDb Method, this make me an error but I can't identify them while the debugger spring.
Here my code:

public void LoadImportedMLeaderStyle()
{
List<AcDataService.MLeaderStyle> mlStyles = new List<AcDataService.MLeaderStyle>();
var doc = AcAp.Application.DocumentManager.MdiActiveDocument;
AcDataService.Database acCurDb = doc.Database;
Dictionary<string, AcDataService.MLeaderStyle> mlStyleDico = new Dictionary<string, AcDataService.MLeaderStyle>();
string dwgFilePath = Symbol.RichtungsPfeileOrSonstigesOrRSADWGPath(Symbol.mLeaderStyleDWGFileName);
if (File.Exists(dwgFilePath))
{
try
{
using (var trans = doc.TransactionManager.StartTransaction())
{
using (AcDataService.Database sourceDb = new AcDataService.Database(false, true))
{
//load MLeaderStyle aus DWG
sourceDb.ReadDwgFile(dwgFilePath, FileShare.Read, false, null);
sourceDb.CloseInput(true);
using (var transsourcedb = sourceDb.TransactionManager.StartOpenCloseTransaction())
{
//Suche die MLeaderStyle
var mlStyleImport = (AcDataService.DBDictionary)transsourcedb.GetObject(sourceDb.MLeaderStyleDictionaryId, AcDataService.OpenMode.ForRead);

                    //Load die aktuell MLeaderStyle aus Current DB
                    AcDataService.DBDictionary mlStylesAktuell = (AcDataService.DBDictionary)trans.GetObject(acCurDb.MLeaderStyleDictionaryId, AcDataService.OpenMode.ForRead);

                    foreach (AcDataService.DBDictionaryEntry entry in mlStyleImport)
                    {
                       AcDataService.MLeaderStyle mls = (AcDataService.MLeaderStyle)transsourcedb.GetObject(entry.Value, AcDataService.OpenMode.ForRead);
                       if (mls != null)
                       {
                          if (!mlStylesAktuell.Contains(mls.Name))
                          {
                             var mlsClone = mls.Clone() as AcDataService.MLeaderStyle;
                             //Gibt den Clone als neue MLeaderStyle an den Current DB
                             mlsClone.PostMLeaderStyleToDb(acCurDb, mls.Name);
                             trans.AddNewlyCreatedDBObject(mlsClone, true);
                          }
                       }
                    }
                    transsourcedb.Commit();
                 }
              }
              trans.Commit();
           }
        }
        catch(Exception ex)
        {

        }
     }

Comments

  • You cannot insert a cloned object into another database. The object is full of links to another database. It doesn't work that way. Copying between drawings is done only by the WblockCloneObjects method

  • avc
    avc
    edited September 2020

    just like example:

        private static void DimStylesImport(Database targetDb, string fileName, string[] DimStylesNames, DuplicateRecordCloning behaviour)
        {
          using Database sourceDb = DocExt.GetDocDatabase(fileName);
          if (sourceDb is null) return;
          if (sourceDb == targetDb) return;        // вызов метода из самого файла шаблона
          ObjectIdCollection dimIds = new ObjectIdCollection();
          using (Transaction sourceTr = sourceDb.TransactionManager.StartTransaction())
          {
            DimStyleTable dst = sourceTr.GetObject(sourceDb.DimStyleTableId, OpenMode.ForRead) as DimStyleTable;
            foreach (string dimName in DimStylesNames)
              if (dst.Has(dimName))
                dimIds.Add(dst[dimName]);
            sourceTr.Commit();
          }
    
          IdMapping mapping = new IdMapping();
          sourceDb.WblockCloneObjects(dimIds, targetDb.DimStyleTableId, mapping, behaviour, false);
        }
    
  • Hi avc,
    thanks for your help.
    With the WBlockCloneObjects it's working perfect.

    Thanks