Transfer/Copy DrawingStyle from one Drawing to another (w/o Styleslib!)

Transfer/Copy DrawingStyle from one Drawing to another (w/o Styleslib!)

c_hoppen
Advocate Advocate
346 Views
2 Replies
Message 1 of 3

Transfer/Copy DrawingStyle from one Drawing to another (w/o Styleslib!)

c_hoppen
Advocate
Advocate

I need to transfer a PartslistStyle from one idw to another. Using the UI you could export/import a .styxml.

As far as I know the API doesn't support working with .styxml.

What I tried:

 

            DrawingDocument targetIDW = invApp.ActiveDocument as DrawingDocument;
            DrawingStylesManager targetStylesMgr = targetIDW.StylesManager;

            DrawingDocument sourceIDW = invApp.Documents.Open(
                @"C:\Users\xxx\yyy.idw",
                false) as DrawingDocument;

            PartsListStyle partsListStyleSource;
            PartsListStyle partsListStyleTarget;

            partsListStyleSource = sourceIDW.StylesManager.PartsListStyles["Bauteilliste Bauteil"];
            try
            {
                partsListStyleTarget = targetStylesMgr.PartsListStyles[partsListStyleSource.Name];
                partsListStyleTarget.Delete();
            }
            catch
            { }

            // First attempt
            partsListStyleTarget = targetStylesMgr.PartsListStyles[1].Copy(partsListStyleSource.Name) as PartsListStyle;            
            partsListStyleTarget = partsListStyleSource;

            // Second attempt
            ObjectCollection replacedStyles = invApp.TransientObjects.CreateObjectCollection();
            replacedStyles.Add(partsListStyleTarget);
            targetStylesMgr.ReplaceStyles(replacedStyles, partsListStyleSource as Style);

 

Both attempts do nothing, there is no exception thrown, nor is the style updated.

 

I there a solution?

 

Thanks,

Christoph

 

Edit: Don't ask why I don't save to library / update from library. The customers lib is *?%!&#. 😉

0 Likes
347 Views
2 Replies
Replies (2)
Message 2 of 3

WCrihfield
Mentor
Mentor

Hi @c_hoppen.  You code looks like C#, which I'm not fluent in, but I think I understand what you are trying to do.  I think that both attempts share the same flaw...you can't set a resource that is local to one document as the value of something in another document.  You were partially on the right track though in your first attempt.  You would basically need to recreate that PartsListStyle within the 'target' document, since copying it directly to another document is not an option, and uploading/downloading is also not an option.  The tricky part of recreating the exact same style in another document is that all the same 'sub-styles' need to also be available in the 'target' document, or it might not work.  Such as what to set as the values of the PartsListStyle's ColumnHeaderTextStyle, DataTextStyle, TitleTextStyle in the 'target' document.  You could probably just make your code a lot more robust, and attempt to copy those over first, then the PartsListStyle, but you get the point.  Below is a quickie attempt at this process in an iLogic rule (vb.net, not C#.net).  The file path & style names are just quick generic examples here.

Sub Main
	Dim oTargetIDW As DrawingDocument = ThisApplication.ActiveDocument
	Dim oSourceIDW As DrawingDocument = ThisApplication.Documents.Open("C:\Temp\MyIDW.idw")
	Dim oPLSName As String = "MyPListStyle1"
	Dim oSourcePLS As PartsListStyle = oSourceIDW.StylesManager.PartsListStyles.Item(oPLSName)
	Dim oTargetPLS As PartsListStyle = Nothing
	Try
		oTargetPLS = oTargetIDW.StylesManager.PartsListStyles.Item(oPLSName)
	Catch
		oTargetPLS = oTargetIDW.StylesManager.PartsListStyles.Item(1).Copy(oPLSName)
		oTargetPLS.ColumnHeaderTextStyle = oSourcePLS.ColumnHeaderTextStyle
		oTargetPLS.Comments = oSourcePLS.Comments
		oTargetPLS.DataTextStyle = oSourcePLS.DataTextStyle
		oTargetPLS.HeadingGap = oSourcePLS.HeadingGap
		oTargetPLS.HeadingPlacement = oSourcePLS.HeadingPlacement
		oTargetPLS.RowGap = oSourcePLS.RowGap
		oTargetPLS.RowLineSpacing = oSourcePLS.RowLineSpacing
		oTargetPLS.ShowTitle = oSourcePLS.ShowTitle
		oTargetPLS.TableDirection = oSourcePLS.TableDirection
		oTargetPLS.Title = oSourcePLS.Title
		oTargetPLS.TitleTextStyle = oSourcePLS.TitleTextStyle
	End Try
	oTargetIDW.Update2(True)
	'oTargetIDW.Save
End Sub

If this solved your problem, or answered your question, please click ACCEPT SOLUTION .
Or, if this helped you, please click (LIKE or KUDOS) 👍.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 3

c_hoppen
Advocate
Advocate

Hi @WCrihfield 

Thanks for the hint. The code above only works for me the first time. When I try to change any (writeable) Property from an existing style, the API throws an exception.

The biggest problem, however, is the columns. I can RemoveAllColumns, AddColumn but I don't see any way to get the columns from my source style. I cannot copy the column settings of the source into the target.

Thanks, Christoph

 

0 Likes