Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Topics - rom1

Pages: [1]
1
Display / Zoom in a new database
« on: August 30, 2012, 07:10:44 AM »
Hi,

A little piece of code in C#.Net in order to define the default view/zoom in a drawing which is not active in the editor. For example, it's useful for a newly created database than you create and save without activating it in the editor.

I share this code because i've spent some hours to find it. Most of the examples we found always concern an open and active document and use the editor.

The 2 main tricks are to use the viewporttablerecord whose name is "*Active" and the method database.UpdateExt() once the viewporttablerecord has been modified.

(Code adapted from this dicussion: http://forums.autodesk.com/t5/NET/Zoom-Extents-on-new-Database/td-p/2070825/highlight/true/page/2 )

Code: [Select]
try
{
    if (sideDb.TileMode == true)// Then 'Model
    {
        ViewportTable vpt = sideTr.GetObject(sideDb.ViewportTableId, OpenMode.ForRead) as ViewportTable;
        ViewportTableRecord vptr = sideTr.GetObject(vpt["*Active"], OpenMode.ForWrite) as ViewportTableRecord;
        Point2d pmin = Point2d.Origin;
        Point2d pmax = new Point2d(3000, 1500);
        vptr.CenterPoint = (pmin + (pmax - pmin) / 2);
        vptr.Height = (pmax.Y - pmin.Y);
        vptr.Width = (pmax.X - pmin.X);
        sideDb.UpdateExt(true);
    }
}
catch { }

2
Blocks / Modify Block Attributes
« on: July 08, 2011, 09:48:24 AM »
Hi,

Here below a sub in order to modify a block attribute.

Code: [Select]
  ''' <summary>
    ''' Modifie la valeur d'un attribut de référence de bloc
    ''' </summary>
    ''' <param name="BlocRefID">ID de la référence de bloc</param>
    ''' <param name="BlocRefAttrLibelle">libellé de l'attribut à modifier</param>
    ''' <param name="BlocRefAttrValeur">valeur de l'attribut à modifier</param>
    ''' <remarks></remarks>
    Public Sub BlocModifierAttribut(ByVal BlocRefID As ObjectId, ByVal BlocRefAttrLibelle As String, ByVal BlocRefAttrValeur As String)
        '=========================================================
        'NOM: BlocModifierAttribut
        'DESCRIPTION: Modifie la valeur d'un attribut de référence de bloc
        'ARGUMENTS: BlocRefID: ID de la référence de bloc
        '               BlocRefAttrLibelle: libellé de l'attribut à modifier
        '               BlocRefAttrValeur: valeur de l'attribut à modifier
        'DATE: 03/11/2010
        '=========================================================
        Using myTrans As Transaction = BlocRefID.Database.TransactionManager.StartTransaction
            Try
                Dim BlocRef As BlockReference = BlocRefID.GetObject(OpenMode.ForRead) 'référence de bloc à partir de son ID
                Dim BlocRefAttrCollection As AttributeCollection = BlocRef.AttributeCollection 'collection des ID d'attributs de la référence de bloc

                If BlocRefAttrCollection.Count > 0 Then
                    For Each BlocRefAttrID As ObjectId In BlocRefAttrCollection 'Boucle pour chaque ID d'attribut de la collection
                        Dim BlocRefAttr As AttributeReference = BlocRefAttrID.GetObject(OpenMode.ForWrite)
                        'on examine si son libellé (tag) est celui recherché
                        If BlocRefAttr.Tag.Equals(BlocRefAttrLibelle, StringComparison.OrdinalIgnoreCase) = True Then
                            'Mise à jour de la valeur
                            BlocRefAttr.TextString = BlocRefAttrValeur
                            Exit For 'sortie boucle
                        End If
                    Next
                End If
            Catch ex As Exception

            Finally
                myTrans.Commit()
            End Try
        End Using
    End Sub

Thanks for your feedback and improvements.


target audience:{beginner}

3
Blocks / Selecting BlockReferences
« on: July 08, 2011, 09:45:07 AM »

target audience:{beginner}

Hi,

Here is a function (which needs improvements) in order to select BlockReferences in PaperSpace (in one or all layouts) depending of the block name and the layer:

Code: [Select]
    ''' <summary>
    ''' Selection des blocs par leur nom
    ''' sur l'onglet courant ou sur tous l'espace papier
    ''' en fonction du nom de calque
    ''' </summary>
    ''' <param name="BlocRefNom">Nom de la référence de bloc</param>
    ''' <param name="DessinEntier">l'onglet courant(false) ou tous l'espace papier(true)</param>
    ''' <param name="Calque">nom du calque</param>
    ''' <returns>Collection d'objectid des blocs</returns>
    ''' <remarks></remarks>
    Public Function F_SelectBlockRefInLayouts(ByVal BlocRefNom As String, ByVal DessinEntier As Boolean, ByVal Calque As String) As ObjectIdCollection
        '=========================================================
        'NOM: GetBlockRefInLayouts
        'DESCRIPTION: Selection des blocs par leur nom sur l'onglet courant ou sur tous l'espace papier
        'ARGUMENTS: BlocRefNom: Nom de la référence de bloc
        'RETOUR:        Collection d'objectid des blocs
        'DATE: 15/11/2010
        '=========================================================
        Dim ids As New ObjectIdCollection()
        Dim db As Database = AcadAp.DocumentManager.MdiActiveDocument.Database

        Using tr As Transaction = db.TransactionManager.StartTransaction()
            Try
                Dim layoutDict As DBDictionary = DirectCast(tr.GetObject(db.LayoutDictionaryId, OpenMode.ForRead), DBDictionary)
                For Each entry As DBDictionaryEntry In layoutDict
                    If entry.Key = "Model" Then Continue For 'espace objet non pris en compte

                    Dim layout As Layout = DirectCast(tr.GetObject(entry.Value, OpenMode.ForRead), Layout)
                    If Not layout.TabSelected And Not DessinEntier Then Continue For 'Selection sur le dessin entier ou l'onglet courant

                    Dim layoutBtr As BlockTableRecord = DirectCast(tr.GetObject(layout.BlockTableRecordId, OpenMode.ForRead, False), BlockTableRecord)
                    If Not layoutBtr.IsAnonymous Then
                        For Each id As ObjectId In layoutBtr
                            Dim blk As BlockReference = TryCast(tr.GetObject(id, OpenMode.ForRead), BlockReference)
                            If Not blk Is Nothing Then

                                If Calque = "" Or blk.Layer = Calque Then 'Selection par calque

                                    If blk.IsDynamicBlock Then  'si bloc dynamique
                                        Dim btr As BlockTableRecord = blk.DynamicBlockTableRecord.GetObject(OpenMode.ForRead)
                                        If btr.Name Like BlocRefNom Then
                                            ids.Add(id)
                                        End If
                                    ElseIf blk.Name Like BlocRefNom Then 'si bloc normal
                                        ids.Add(id)
                                    End If
                                End If
                            End If
                        Next
                    End If
                Next
            Catch ex As Exception
                Dim Msg As String = String.Format("Erreur en tentant de sélectionner les blocs {0}", BlocRefNom)
                Dim Titre As String = "Opération annulée"
                MessageBox.Show(Msg, Titre, MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1)
                tr.Abort()
                Return ids
            Finally
                tr.Commit()
            End Try
        End Using

        Return ids

    End Function

(Comments are in French, i'll change it soon)
Thanks for your feedback

4
Layouts and printing / Create a Layout and Set PlotConfiguration
« on: November 26, 2010, 11:33:14 AM »
Hi,

Here is a function (in vb.net) that i've created thanks to some examples and that i've adapted to my needs.
The function create a new layout with the name in parameter (the name is incremented with a counter if it already exists) and then the function change the plot settings (stylesheet, format, rotation, printername...)

Thanks for your remarks and improvements as i'm still a beginner with the .Net api.

Code: [Select]
''' <summary>
    ''' Create a new layout and set the layout plotsettings
    ''' </summary>
    ''' <param name="LayName">Layout Name</param>
    ''' <remarks></remarks>
    Public Sub CreateLayout(ByVal LayName As String)
        Dim ed As Editor = AcadAp.DocumentManager.MdiActiveDocument.Editor
        Dim db As Database = HostApplicationServices.WorkingDatabase

        Using tr As Transaction = db.TransactionManager.StartTransaction()
            Try
                Dim layouts As DBDictionary = DirectCast(tr.GetObject(db.LayoutDictionaryId, OpenMode.ForWrite), DBDictionary)

                ' Add a number at the end of layout name if already exists
                Dim i As Integer = 0
                Dim OriginalName As String = LayName
                Do While layouts.Contains(LayName)
                    i += 1
                    LayName = OriginalName & "-" & i
                Loop
                ' Create and activate the new layout
                Dim layoutId As ObjectId = LayoutManager.Current.CreateLayout(LayName)
                LayoutManager.Current.CurrentLayout = LayName

                'Plot Settings
                '' Get the PlotInfo from the layout
                Dim acLayout As Layout = DirectCast(tr.GetObject(layoutId, OpenMode.ForRead), Layout)
                Dim acPlInfo As PlotInfo = New PlotInfo()
                acPlInfo.Layout = acLayout.ObjectId

                '' Get a copy of the PlotSettings from the layout
                Dim acPlSet As PlotSettings = New PlotSettings(acLayout.ModelType)
                acPlSet.CopyFrom(acLayout)

                '' Update some properties of the PlotSettings object
                Dim acPlSetVdr As PlotSettingsValidator = PlotSettingsValidator.Current
                acPlSetVdr.SetPlotConfigurationName(acPlSet, "\\dani-print2\BE-Blanc_ps", "A4")
                acPlSetVdr.SetPlotType(acPlSet, Autodesk.AutoCAD.DatabaseServices.PlotType.Layout)
                acPlSetVdr.SetUseStandardScale(acPlSet, True)
                acPlSetVdr.SetPlotRotation(acPlSet, Autodesk.AutoCAD.DatabaseServices.PlotRotation.Degrees000)
                acPlSetVdr.SetCurrentStyleSheet(acPlSet, "Danisoft.ctb")

                '' Update the layout
                acLayout.UpgradeOpen()
                acLayout.CopyFrom(acPlSet)

                tr.Commit() 'Commit Transaction
            Catch ex As System.Exception
                Dim Msg As String = "Error in function CreateLayout"
                Dim Title As String = "Operation cancelled"
                MessageBox.Show(Msg, Title, MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1)
                tr.Abort()
            End Try
        End Using
    End Sub



target audience:{beginner}

5
F# language / Code Converter for F#
« on: November 24, 2010, 07:50:23 AM »
Hi,

Same question than for C# and VB:

Do you know good code converters for F# to C# or F# to VB?

Thanks


target audience:{advanced}

6
Blocks / Erase doubled block references
« on: November 23, 2010, 08:16:23 AM »

Hi,

I need some help in order to use overrides function for 'Equals' and 'GetHashCode' into a structure:

You'll find enclose a sample function which erase the block references when they have the same name, layer, insertion point, scale and angle.

For that function, i wanted to use a dictionary in order to compare those parameters by using a structure.
This function seems to work this way, however, i think i have to improve the structure i used in order to compare the parameters exactly with the criteria i want but i don't really understand how to use override for the GetHashCode function.

Your help would be very appreciate.
Thanks.



target audience:{intermediate}

7
Layouts and printing / Create and copy Layouts?
« on: November 23, 2010, 07:48:26 AM »

Hi,

If you had some examples in order to create a new layout and insert some elements into or in order to copy an existing layout, that would be great since i have some difficulties in order to find good examples.

Thanks!

target audience:{advanced}

8
Layouts and printing / Get the current layout
« on: November 23, 2010, 07:45:21 AM »
Hi,

Here is a sample function in order to get back the current layout:

Code: [Select]

    Function F_GetCurrentLayout() As Layout
        ' Get the current document and database, and start a transaction '
        Dim acCurDb As Database = AcadAp.DocumentManager.MdiActiveDocument.Database
        Dim acLayoutMgr As LayoutManager
        Dim acLayout As Layout = Nothing

        Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()
            Try
                acLayoutMgr = LayoutManager.Current ' Reference the Layout Manager
                ' Get the current layout
                acLayout = acTrans.GetObject(acLayoutMgr.GetLayoutId(acLayoutMgr.CurrentLayout), OpenMode.ForRead)

            Catch ex As Exception
                Dim Msg As String = "Error in function F_GetCurrentLayout"
                Dim Titre As String = "Operation cancelled"
                MessageBox.Show(Msg, Titre, MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1)
                acTrans.Abort()

            Finally
                ' Close transaction '
                acTrans.Dispose()
            End Try
        End Using

        Return acLayout
    End Function

Thanks for your remarks.


target audience:{intermediate}

9
Layers / Activate/Unfreeze Layers
« on: November 18, 2010, 10:49:15 AM »
Hi,

A little function in vb in order to make a layer active and not frozen:

Code: [Select]

    Sub LayerActiveUnfreeze(ByVal LayerName As String)

        Dim DocBd As Database = ApplicationServices.Application.DocumentManager.MdiActiveDocument.Database

        Using myTrans As Transaction = DocBd.TransactionManager.StartTransaction
            Try
                Dim myLayerTable As LayerTable = DocBd.LayerTableId.GetObject(OpenMode.ForRead)
                For Each myLTRid As ObjectId In myLayerTable
                    Dim myLayer As LayerTableRecord = myLTRid.GetObject(OpenMode.ForRead)
                    If myLayer.Name Like LayerName Then
                        If myLayer.IsFrozen Then
                            myLayer.UpgradeOpen()
                            myLayer.IsFrozen = False
                        End If
                        If myLayer.IsLocked Then
                            myLayer.UpgradeOpen()
                            myLayer.IsLocked = False
                        End If
                    End If
                Next
            Catch ex As Exception
                myTrans.Abort()
            Finally
                myTrans.Commit()
            End Try
        End Using
    End Sub

target audience:{intermediate}

10
Layouts and printing / Check Paper/Model Space
« on: November 18, 2010, 10:40:12 AM »
Hi,

My 1st little contribution

A function to check which of Paper/Model Space is active:

Code: [Select]

    Function F_TestPaperSpace() As Boolean

        If LayoutManager.Current.CurrentLayout = "Model" Then
            F_TestPaperSpace = False
        Else
            F_TestPaperSpace = True
        End If
    End Function

target audience:{beginner}

Pages: [1]