TheSwamp
Code Red => .NET => Topic started by: WOWENS 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?
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
-
Read this topic (http://www.theswamp.org/index.php?topic=4429.0), please.
-
thanks for letting me know about the link "Formatting your code for best results at the TheSwamp.org"
-
See if this helps, cnange source file name and mleaderstyle name for your test:
[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;
}
}
}
-
thank you for your help, I will give this a try when i get back to work after the holidays.
-
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.
-
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
-
Thank you for all your help.
-
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.
-
This is aimed more at Fixo than you.
Do you suggest that DuplicateRecordCloning.MangleName will do the job instead?
-
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.
-
if you were wondering this is how i worked around the update \ change existing MLeaderStyle..
<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
-
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).
-
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?
-
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.
-
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.
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