Author Topic: cloning MLeaderStyle  (Read 4812 times)

0 Members and 1 Guest are viewing this topic.

WOWENS

  • Newt
  • Posts: 59
cloning MLeaderStyle
« on: November 20, 2012, 05:05:27 PM »
This is what i have so far, it does import the MLeaderStyle from the Source Drawing but the MLeaderStyle in the Target drawing does not update or change.
My Question is What am I doing wrong? or what am i missing?

Code: [Select]
Public Function CloneMLeaderStyle(ByVal sourceFileName As String, ByVal TargetDataBase As Database, ByVal sMLeaderStyleName As String) As Boolean

        Using sourceDatabase As New Database(False, True)
            sourceDatabase.ReadDwgFile(sourceFileName, FileOpenMode.OpenForReadAndAllShare, True, Nothing)

            Try
                 Using sourceDbTr As Transaction = sourceDatabase.TransactionManager.StartTransaction()
                    Try
                        Dim dict As DBDictionary = TryCast(sourceDbTr.GetObject(sourceDatabase.MLeaderStyleDictionaryId, OpenMode.ForRead), DBDictionary)

                        Dim sourceMLeaderStyleID As ObjectId = ObjectId.Null
                        If dict.Contains(sMLeaderStyleName) Then
                            sourceMLeaderStyleID = dict.GetAt(sMLeaderStyleName)
                        Else
                            Return False
                        End If

                        Dim sourceMLeaderStyleIds As New ObjectIdCollection()
                        sourceMLeaderStyleIds.Add(sourceMLeaderStyleID)

                        Dim dimStyleIdMap As New IdMapping()
                        TargetDataBase.WblockCloneObjects(sourceMLeaderStyleIds, TargetDataBase.MLeaderStyleDictionaryId, dimStyleIdMap, DuplicateRecordCloning.Replace, False)

                        sourceDbTr.Commit()
                    Catch
                        Return False
                    End Try
                End Using
            Catch
                Return False
            End Try

        End Using

        Return True
End Function
« Last Edit: November 21, 2012, 10:04:50 AM by WOWENS »

Andrey Bushman

  • Swamp Rat
  • Posts: 864
Re: cloning MLeaderStyle
« Reply #1 on: November 21, 2012, 12:46:44 AM »
Read this topic, please.

WOWENS

  • Newt
  • Posts: 59
Re: cloning MLeaderStyle
« Reply #2 on: November 21, 2012, 10:04:10 AM »
thanks for letting me know about the link "Formatting your code for best results at the TheSwamp.org"

fixo

  • Guest
Re: cloning MLeaderStyle
« Reply #3 on: November 21, 2012, 01:52:46 PM »
See if this helps, cnange source file name and mleaderstyle name for your test:


Code: [Select]
          [CommandMethod("cml")]
        public void copyMleaderStyle()
        {
            string sourceName =   @"c:\test\mysourcedrawing.dwg";
            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Editor ed = doc.Editor;
            Database db = doc.Database;
            CloneMLeaderStyle(db, sourceName, "MyStyle");

        }
       

        public ObjectId CloneMLeaderStyle(Database targetDb, string sourceFile, string mLeaderStyleName)
        {
            using (Database sourceDb = new Database(true,false))
            {
                sourceDb.ReadDwgFile(sourceFile, System.IO.FileShare.Read,false, "");

                 using (Transaction tr = sourceDb.TransactionManager.StartTransaction())
                {
                    DBDictionary dict = tr.GetObject(sourceDb.MLeaderStyleDictionaryId, OpenMode.ForRead) as DBDictionary;

                    ObjectId mlStyleID = ObjectId.Null;

                    if (!dict.Contains(mLeaderStyleName))
                    {
                        mlStyleID = dict.GetAt(mLeaderStyleName);

                        return ObjectId.Null;
                    }               
               
                    else
                    {
                    mlStyleID = dict.GetAt(mLeaderStyleName);

                    ObjectIdCollection ids = new ObjectIdCollection();
                   
                    ids.Add(mlStyleID);

                    IdMapping idMap = new IdMapping();

                    sourceDb.WblockCloneObjects(ids, targetDb.MLeaderStyleDictionaryId, idMap, DuplicateRecordCloning.Ignore, false);

                    tr.Commit();

                    mlStyleID = idMap[mlStyleID].Value;
                   
                    }
                    return mlStyleID;
                }
            }
        }

WOWENS

  • Newt
  • Posts: 59
Re: cloning MLeaderStyle
« Reply #4 on: November 21, 2012, 02:19:52 PM »
thank you for your help, I will give this a try when i get back to work after the holidays.

WOWENS

  • Newt
  • Posts: 59
Re: cloning MLeaderStyle
« Reply #5 on: November 25, 2012, 01:07:20 PM »
sorry to say but your code does the same as mine..it will clone the Mleaderstyle form one drawing to the other but if the Target database allready has that MLeaderStyle then it does not update the MLeaderStyle with the new one.

fixo

  • Guest
Re: cloning MLeaderStyle
« Reply #6 on: November 25, 2012, 03:29:34 PM »
Try before to check if target drawing already has this style
if so then purge them from there, then clone new one,
just a guess, sorry

WOWENS

  • Newt
  • Posts: 59
Re: cloning MLeaderStyle
« Reply #7 on: November 25, 2012, 04:22:40 PM »
Thank you for all your help.

TheMaster

  • Guest
Re: cloning MLeaderStyle
« Reply #8 on: November 25, 2012, 09:08:59 PM »
sorry to say but your code does the same as mine..it will clone the Mleaderstyle form one drawing to the other but if the Target database allready has that MLeaderStyle then it does not update the MLeaderStyle with the new one.

sourceDb.WblockCloneObjects(ids, targetDb.MLeaderStyleDictionaryId, idMap, DuplicateRecordCloning.Ignore, false);


This is aimed more at Fixo than you.  To do this kind of stuff you have to be paying a bit more attention than you are.

You can't just wholesale copy and paste code from samples you find here and there, without making a reasonable effort to understand the code you're pasting into a project, how it works, and for example, what each argument actually means.








fixo

  • Guest
Re: cloning MLeaderStyle
« Reply #9 on: November 26, 2012, 05:09:38 AM »

This is aimed more at Fixo than you. 

Do you suggest that DuplicateRecordCloning.MangleName will do the job instead?

WOWENS

  • Newt
  • Posts: 59
Re: cloning MLeaderStyle
« Reply #10 on: November 26, 2012, 08:15:33 AM »
I do have one question.
why sourceDatabase.WblockCloneObjects instead of TargetDatabase.WblockCloneObjects?
(the reason i ask is, I have a book writen by jerry winters and he uses TargetDatabase.WblockCloneObjects instead of sourceDatabase.WblockCloneObjects )
and as for DuplicateRecordCloning.MangleName this will clone the MLeaderStyle from the sourceDatabase to the TargetDatabase but it
renames the MLeaderStyle when it brings it in, what im trying to do is overright the Standard MLeaderStyle with another Standard MLeaderStyle.
« Last Edit: November 26, 2012, 09:46:03 AM by WOWENS »

WOWENS

  • Newt
  • Posts: 59
Re: cloning MLeaderStyle
« Reply #11 on: November 26, 2012, 05:28:57 PM »
if you were wondering this is how i worked around the update \ change existing MLeaderStyle..

Code: [Select]
<CommandMethod("cml")> _
        Public Sub CloneCopyMleaderStyle()

            Dim sourceName As String = BS_Acad.GetAutoCadPath + "\Braden_Support\ProgramBlocks\BrdnDimStyle.dwg"
            Dim mlStyle As String = "standard"

            Dim doc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
            Dim db As Database = doc.Database

            Dim mlStyleID As ObjectId = CloneMLeaderStyle(db, sourceName, mlStyle, db.Dimscale)

        End Sub

       
        Public Function CloneMLeaderStyle(ByVal TargetDatabase As Database, ByVal sourceFileName As String, ByVal mLeaderStyleName As String, ByVal dscale As Double) As ObjectId

            Using TargetTrans As Transaction = TargetDatabase.TransactionManager.StartTransaction

                Dim ThisDrawing As Autodesk.AutoCAD.Interop.AcadApplication = Autodesk.AutoCAD.ApplicationServices.Application.AcadApplication

                Dim Targetdict As DBDictionary = TryCast(TargetTrans.GetObject(TargetDatabase.MLeaderStyleDictionaryId, OpenMode.ForWrite), DBDictionary)

                Using sourceDatabase As New Database(False, True)
                    sourceDatabase.ReadDwgFile(sourceFileName, FileOpenMode.OpenForReadAndAllShare, True, Nothing)
                    Try
                        Using sourceDbTr As Transaction = sourceDatabase.TransactionManager.StartTransaction()
                            Try
                                Dim Sourcedict As DBDictionary = TryCast(sourceDbTr.GetObject(sourceDatabase.MLeaderStyleDictionaryId, OpenMode.ForRead), DBDictionary)

                                If Not Sourcedict.Contains(mLeaderStyleName) Then
                                    Return ObjectId.Null
                                End If

                                Dim SourceMLeaderStyleID As ObjectId = Sourcedict.GetAt(mLeaderStyleName)
                                Dim ids As New ObjectIdCollection()

                                ids.Add(SourceMLeaderStyleID)
                                Dim idMap As New IdMapping()

                                If Not Targetdict.Contains(mLeaderStyleName) Then
                                    TargetDatabase.WblockCloneObjects(ids, TargetDatabase.MLeaderStyleDictionaryId, idMap, DuplicateRecordCloning.Replace, False)

                                    ThisDrawing.ActiveDocument.SetVariable("MLeaderScale", dscale)

                                    TargetTrans.Commit()

                                    Return idMap(SourceMLeaderStyleID).Value
                                Else
                                    TargetDatabase.WblockCloneObjects(ids, TargetDatabase.MLeaderStyleDictionaryId, idMap, DuplicateRecordCloning.MangleName, False)

                                    Dim TempMLeaderStyleID As ObjectId = idMap(SourceMLeaderStyleID).Value
                                    Dim TempMLeaderStyle As MLeaderStyle = TryCast(TargetTrans.GetObject(TempMLeaderStyleID, OpenMode.ForRead), MLeaderStyle)

                                    Dim TargetMLeaderStyleID As ObjectId = Targetdict.GetAt(mLeaderStyleName)
                                    Dim TargetMLeaderStyle As New MLeaderStyle
                                    TargetMLeaderStyle = TryCast(TargetTrans.GetObject(TargetMLeaderStyleID, OpenMode.ForRead), MLeaderStyle)

                                    TargetMLeaderStyle.UpgradeOpen()

                                    TargetMLeaderStyle.ArrowSize = TempMLeaderStyle.ArrowSize
                                    TargetMLeaderStyle.ArrowSymbolId = TempMLeaderStyle.ArrowSymbolId
                                    TargetMLeaderStyle.ContentType = TempMLeaderStyle.ContentType

                                    TargetMLeaderStyle.LeaderLineType = TempMLeaderStyle.LeaderLineType
                                    TargetMLeaderStyle.ExtendLeaderToText = TempMLeaderStyle.ExtendLeaderToText
                                    TargetMLeaderStyle.LandingGap = TempMLeaderStyle.LandingGap

                                    TargetMLeaderStyle.LeaderLineColor = TempMLeaderStyle.LeaderLineColor
                                    TargetMLeaderStyle.LeaderLineType = TempMLeaderStyle.LeaderLineType
                                    TargetMLeaderStyle.LeaderLineTypeId = TempMLeaderStyle.LeaderLineTypeId
                                    TargetMLeaderStyle.LeaderLineWeight = TempMLeaderStyle.LeaderLineWeight
                                    TargetMLeaderStyle.DoglegLength = TempMLeaderStyle.DoglegLength
                                    TargetMLeaderStyle.DrawLeaderOrderType = TempMLeaderStyle.DrawLeaderOrderType
                                    TargetMLeaderStyle.DrawMLeaderOrderType = TempMLeaderStyle.DrawMLeaderOrderType
                                    TargetMLeaderStyle.MaxLeaderSegmentsPoints = TempMLeaderStyle.MaxLeaderSegmentsPoints

                                    TargetMLeaderStyle.FirstSegmentAngleConstraint = TempMLeaderStyle.FirstSegmentAngleConstraint
                                    TargetMLeaderStyle.SecondSegmentAngleConstraint = TempMLeaderStyle.SecondSegmentAngleConstraint

                                    TargetMLeaderStyle.TextAlignAlwaysLeft = TempMLeaderStyle.TextAlignAlwaysLeft
                                    TargetMLeaderStyle.TextAngleType = TempMLeaderStyle.TextAngleType
                                    TargetMLeaderStyle.TextHeight = TempMLeaderStyle.TextHeight
                                    TargetMLeaderStyle.TextColor = TempMLeaderStyle.TextColor
                                    TargetMLeaderStyle.TextStyleId = TempMLeaderStyle.TextStyleId
                                    TargetMLeaderStyle.TextAttachmentType = TempMLeaderStyle.TextAttachmentType
                                    TargetMLeaderStyle.TextAttachmentDirection = TempMLeaderStyle.TextAttachmentDirection
                                    TargetMLeaderStyle.TextAlignmentType = TempMLeaderStyle.TextAlignmentType
                                    TargetMLeaderStyle.DefaultMText = TempMLeaderStyle.DefaultMText
                                    TargetMLeaderStyle.Annotative = TempMLeaderStyle.Annotative

                                    TargetMLeaderStyle.BlockId = TempMLeaderStyle.BlockId
                                    TargetMLeaderStyle.BlockConnectionType = TempMLeaderStyle.BlockConnectionType
                                    TargetMLeaderStyle.BlockRotation = TempMLeaderStyle.BlockRotation
                                    TargetMLeaderStyle.BlockScale = TempMLeaderStyle.BlockScale
                                    TargetMLeaderStyle.BlockColor = TempMLeaderStyle.BlockColor
                                    TargetMLeaderStyle.EnableLanding = TempMLeaderStyle.EnableLanding
                                    TargetMLeaderStyle.EnableBlockRotation = TempMLeaderStyle.EnableBlockRotation
                                    TargetMLeaderStyle.EnableBlockScale = TempMLeaderStyle.EnableBlockScale
                                    TargetMLeaderStyle.EnableDogleg = TempMLeaderStyle.EnableDogleg
                                    TargetMLeaderStyle.EnableFrameText = TempMLeaderStyle.EnableFrameText

                                    TargetMLeaderStyle.BreakSize = TempMLeaderStyle.BreakSize

                                    TargetMLeaderStyle.SetTextAttachmentType(TempMLeaderStyle.GetTextAttachmentType(LeaderDirectionType.LeftLeader), LeaderDirectionType.LeftLeader)
                                    TargetMLeaderStyle.SetTextAttachmentType(TempMLeaderStyle.GetTextAttachmentType(LeaderDirectionType.RightLeader), LeaderDirectionType.RightLeader)
                                    TargetMLeaderStyle.SetTextAttachmentType(TempMLeaderStyle.GetTextAttachmentType(LeaderDirectionType.UnknownLeader), LeaderDirectionType.UnknownLeader)

                                    TargetMLeaderStyle.Scale = dscale

                                    TargetMLeaderStyle.DowngradeOpen()

                                    Targetdict.Remove(TempMLeaderStyleID)

                                    ThisDrawing.ActiveDocument.SetVariable("MLeaderScale", dscale)

                                    TargetTrans.Commit()

                                    Return Targetdict.GetAt(mLeaderStyleName)
                                End If
                            Catch
                                Return ObjectId.Null
                            End Try
                        End Using
                    Catch
                        Return ObjectId.Null
                    End Try
                End Using
                System.Runtime.InteropServices.Marshal.ReleaseComObject(ThisDrawing)
            End Using

        End Function

TheMaster

  • Guest
Re: cloning MLeaderStyle
« Reply #12 on: November 26, 2012, 09:48:47 PM »
I do have one question.
why sourceDatabase.WblockCloneObjects instead of TargetDatabase.WblockCloneObjects?
(the reason i ask is, I have a book writen by jerry winters and he uses TargetDatabase.WblockCloneObjects instead of sourceDatabase.WblockCloneObjects )
and as for DuplicateRecordCloning.MangleName this will clone the MLeaderStyle from the sourceDatabase to the TargetDatabase but it
renames the MLeaderStyle when it brings it in, what im trying to do is overright the Standard MLeaderStyle with another Standard MLeaderStyle.

Have you tried DuplicateRecordCloning.Replace ?

If it doesn't work, then the standard workaround is to first rename the existing record, clone the new one, and then swap the objectids of the new and existing records (using SwapIdsWith() after cloning).

According to the docs, you call WBlockCloneObjects() on the destination database (the objects to be cloned can be from one or more source databases).
« Last Edit: November 26, 2012, 09:52:04 PM by TT »

Jeff H

  • Needs a day job
  • Posts: 6144
Re: cloning MLeaderStyle
« Reply #13 on: November 27, 2012, 12:25:11 AM »
Through the UI as in using Design Center I think only blocks have the ability to be redefined if that has anything to do with built in functionality.
Also and have not tested but a DBDictionary has a MergeStyle property and not sure if setting the MleaderStyleDictionary mergestyle to Replace would do anything?

WOWENS

  • Newt
  • Posts: 59
Re: cloning MLeaderStyle
« Reply #14 on: November 27, 2012, 07:50:19 AM »
yes i have tried DuplicateRecordCloning.Replace but did not work as i thought it should.
Thank you all for more Idea's I will look at them today.

WOWENS

  • Newt
  • Posts: 59
Re: cloning MLeaderStyle
« Reply #15 on: November 27, 2012, 10:15:14 AM »
Thank you TT, your Idea was way Better then mine. I hope I understood the correct way of doing it.
and Jeff thank you for your Idea but I was not able to do as you suggested (probably my fault for not know the correct way)
once again thank you all for all your help.

Code: [Select]
Public Function CloneMLeaderStyle(ByVal TargetDatabase As Database, ByVal sourceFileName As String, ByVal mLeaderStyleName As String, ByVal dscale As Double) As ObjectId

            If IO.File.Exists(sourceFileName) = False Or mLeaderStyleName = "" Then
                Return ObjectId.Null
            End If

            Using TargetTrans As Transaction = TargetDatabase.TransactionManager.StartTransaction
                Dim Targetdict As DBDictionary = TryCast(TargetTrans.GetObject(TargetDatabase.MLeaderStyleDictionaryId, OpenMode.ForRead), DBDictionary)

                Using sourceDatabase As New Database(False, True)
                    sourceDatabase.ReadDwgFile(sourceFileName, FileOpenMode.OpenForReadAndAllShare, True, Nothing)
                    Using sourceDbTr As Transaction = sourceDatabase.TransactionManager.StartTransaction()
                        Try
                            Dim Sourcedict As DBDictionary = TryCast(sourceDbTr.GetObject(sourceDatabase.MLeaderStyleDictionaryId, OpenMode.ForRead), DBDictionary)

                            If Not Sourcedict.Contains(mLeaderStyleName) Then
                                Return ObjectId.Null
                            End If

                            Dim SourceMLeaderStyleID As ObjectId = Sourcedict.GetAt(mLeaderStyleName)
                            Dim ids As New ObjectIdCollection()

                            ids.Add(SourceMLeaderStyleID)
                            Dim idMap As New IdMapping()

                            If Not Targetdict.Contains(mLeaderStyleName) Then
                                TargetDatabase.WblockCloneObjects(ids, TargetDatabase.MLeaderStyleDictionaryId, idMap, DuplicateRecordCloning.Replace, False)

                                TargetTrans.Commit()

                                Return idMap(SourceMLeaderStyleID).Value
                            Else
                                TargetDatabase.WblockCloneObjects(ids, TargetDatabase.MLeaderStyleDictionaryId, idMap, DuplicateRecordCloning.MangleName, False)
                                Dim TempMLeaderStyleID As ObjectId = idMap(SourceMLeaderStyleID).Value
                                Dim TempMLeaderStyle As MLeaderStyle = TryCast(TargetTrans.GetObject(TempMLeaderStyleID, OpenMode.ForRead), MLeaderStyle)

                                Dim TargetMLeaderStyleID As ObjectId = Targetdict.GetAt(mLeaderStyleName)
                                Dim TargetMLeaderStyle As MLeaderStyle = TryCast(TargetTrans.GetObject(TargetMLeaderStyleID, OpenMode.ForRead), MLeaderStyle)

                                TempMLeaderStyle.UpgradeOpen()
                                TempMLeaderStyle.Scale = dscale
                                TempMLeaderStyle.DowngradeOpen()

                                TargetMLeaderStyle.UpgradeOpen()
                                TargetMLeaderStyle.SwapIdWith(TempMLeaderStyleID, True, True)
                                TargetMLeaderStyle.DowngradeOpen()

                                Targetdict.Remove(TempMLeaderStyleID)

                                TargetTrans.Commit()

                                Return Targetdict.GetAt(mLeaderStyleName)
                            End If
                        Catch
                            Return ObjectId.Null
                        End Try
                    End Using
                End Using
            End Using
        End Function
« Last Edit: November 27, 2012, 10:29:56 AM by WOWENS »