.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Example: Page Setup from Template VBNET and C#

4 REPLIES 4
Reply
Message 1 of 5
Anonymous
1802 Views, 4 Replies

Example: Page Setup from Template VBNET and C#

The other Post was getting full

 

I threw this together quick, but it will bring in the plotsettings from an external drawing. It does not set it to the current page setup. Will mess with that tommorrow. you could add a for each loop if more than one plotsetting.

 

I converted it to C# in Reflector. I can look at C# and basiclly tell what it is doing but not well enough to tell if it converted correctly except for there are no using statements.

Of course change the file path and page setup name

 

 

Imports Autodesk.AutoCAD
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.ApplicationServices.Application
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.EditorInput
Imports Autodesk.AutoCAD.Geometry
Imports Autodesk.AutoCAD.Runtime
Public Class Class1
    Public Const PLOTSETTINGSFILE As String = "C:\Users\Jeff\Desktop\E201AE60_-_Standard\E201AE60.dwg"
    <CommandMethod("AddPlotSetting")> _
    Public Sub AddPlotSetting()
        Dim curDB As Database = HostApplicationServices.WorkingDatabase
        Dim srcDB As New Database(False, True)
        srcDB.ReadDwgFile(PLOTSETTINGSFILE, FileOpenMode.OpenForReadAndAllShare, True, "")
        ClonePlot(srcDB, curDB)
    End Sub

    Public Function ClonePlot(ByVal SourceDatabase As Database, ByVal CurrentDatabase As Database)
        Using currentTransaction As Transaction = CurrentDatabase.TransactionManager.StartTransaction
            Using sourceTransaction As Transaction = SourceDatabase.TransactionManager.StartTransaction
                Dim sourcePlotDic As DBDictionary = SourceDatabase.PlotSettingsDictionaryId.GetObject(OpenMode.ForRead)
                Dim objID As ObjectId
                If sourcePlotDic.Contains("HCS-half") Then
                    objID = sourcePlotDic.GetAt("HCS-half")
                    Dim pl As PlotSettings = objID.GetObject(OpenMode.ForRead)
                    Dim cpl As New PlotSettings(False)
                    cpl.CopyFrom(pl)
                    cpl.AddToPlotSettingsDictionary(CurrentDatabase)
                End If
                currentTransaction.Commit()
            End Using
        End Using
    End Function
End Class
 
 
public class Class1
{
    // Fields
    public const string PLOTSETTINGSFILE = @"C:\Users\Jeff\Desktop\E201AE60_-_Standard\E201AE60.dwg";

    // Methods
    [CommandMethod("AddPlotSetting")]
    public void AddPlotSetting()
    {
        Database curDB = HostApplicationServices.WorkingDatabase;
        Database srcDB = new Database(false, true);
        srcDB.ReadDwgFile(@"C:\Users\Jeff\Desktop\E201AE60_-_Standard\E201AE60.dwg", FileOpenMode.OpenForReadAndAllShare, true, "");
        this.ClonePlot(srcDB, curDB);
    }

    public object ClonePlot(Database SourceDatabase, Database CurrentDatabase)
    {
        using (Transaction currentTransaction = CurrentDatabase.TransactionManager.StartTransaction())
        {
            using (SourceDatabase.TransactionManager.StartTransaction())
            {
                object ClonePlot;
                DBDictionary sourcePlotDic = (DBDictionary) SourceDatabase.PlotSettingsDictionaryId.GetObject(OpenMode.ForRead);
                if (sourcePlotDic.Contains("HCS-half"))
                {
                    PlotSettings pl = (PlotSettings) sourcePlotDic.GetAt("HCS-half").GetObject(OpenMode.ForRead);
                    PlotSettings cpl = new PlotSettings(false);
                    cpl.CopyFrom(pl);
                    cpl.AddToPlotSettingsDictionary(CurrentDatabase);
                }
                currentTransaction.Commit();
                return ClonePlot;
            }
        }
    }
}

 

 

4 REPLIES 4
Message 2 of 5
Anonymous
in reply to: Anonymous

Thanks for the code! I like the way you got this far, short and sweet, and I hope you can work out getting the copied PlotSettings object to be current. I am also working on it, but it just isn't taking even with refetching the PSO's id from the destination dictionary and using SetDefaultPlotConfig multiple times before and after modification. I am just trying to find the sequence of actions which will get it to take hold and stay where it is put. Your help is appreciated and hopefully I can return the favor.

Message 3 of 5
Anonymous
in reply to: Anonymous

I am just starting to write .net code for autocad

please let me know if there is a better way or design pattern for this task

 

 

This will update only one paperspace  layout

If you have more then one it updates the first paperspace layout

You will probably notice you want the viewport updated so it might be better to clone(if you can?) or copy a layout from a external drawing. I do not know yet if clonelayout works with external drawings.

 

!!!!! Do not let a beginner like me get you in trouble with your plotting. This does not do any checking or validating if you have the correct plot style table (if .ctb) or printer  etc.!!!!!!!!

 

Also ClonePlot is not a good name for the function CopyPlotSettings is probably better

 

Here is the updated function 

 

  Public Function ClonePlot(ByVal SourceDatabase As Database, ByVal CurrentDatabase As Database)
        Using currentTransaction As Transaction = CurrentDatabase.TransactionManager.StartTransaction
            Using sourceTransaction As Transaction = SourceDatabase.TransactionManager.StartTransaction
                Dim sourcePlotDic As DBDictionary = SourceDatabase.PlotSettingsDictionaryId.GetObject(OpenMode.ForRead)
                Dim objID As ObjectId
                If sourcePlotDic.Contains("HCS-half") Then
                    objID = sourcePlotDic.GetAt("HCS-half")
                    Dim pl As PlotSettings = objID.GetObject(OpenMode.ForRead)
                    Dim cpl As New PlotSettings(False)
                    cpl.CopyFrom(pl)
                    cpl.AddToPlotSettingsDictionary(CurrentDatabase)


                    '''''''''''''''''''Updated code'''''''''''''''''''''''''
                    Dim bt As BlockTable = CurrentDatabase.BlockTableId.GetObject(OpenMode.ForRead)
                    Dim btr As BlockTableRecord = bt(BlockTableRecord.PaperSpace).GetObject(OpenMode.ForRead)
                    Dim lytobjID As ObjectId = btr.LayoutId.GetObject(OpenMode.ForRead).ObjectId
                    Dim lytps As PlotSettings = lytobjID.GetObject(OpenMode.ForWrite)
                    lytps.CopyFrom(cpl)
                    ''Regen the screen
                    DocumentManager.MdiActiveDocument.Editor.UpdateScreen()
                    ''Doing a zoom all 
                    DocumentManager.MdiActiveDocument.SendStringToExecute("_Zoom A ", True, False, False)
                    '''''''''''''''''''Updated code'''''''''''''''''''''''''


                End If
                currentTransaction.Commit()
            End Using
        End Using
    End Function

 

 

Message 4 of 5
kgallagherCGHDN
in reply to: Anonymous

this looks good but i need to copy ALL the pagesetups from a template, thoughts

Message 5 of 5
Ed.Jobe
in reply to: kgallagherCGHDN

@kgallagherCGHDN  Please start a new thread for your request.

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost