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

Accessing AutoCAD FIELDS and editing via VB.NET

3 REPLIES 3
SOLVED
Reply
Message 1 of 4
dennis
2352 Views, 3 Replies

Accessing AutoCAD FIELDS and editing via VB.NET

I have attached a DOC file that shows exactly the Plant 3D interface and the reference to the FIELD that I am after.  If someone can point me in the right direction, I want to access a FIELD (specifically the AREA field in an AutoCAD PLANT 3D drawing), and then set that FIELD value.  

3 REPLIES 3
Message 2 of 4
AubelecBE
in reply to: dennis

        'insertion du bloc !!!
        Dim RefBloc As BlockReference = Nothing
        Dim BlocT As BlockTable
        Dim CollObject As New Collection

        tr = db.TransactionManager.StartTransaction
        Try
            Dim ang As Object
            Dim CoteA, CoteB As Double
            Dim pointUcs As New Point3d(0, 500, 0)
            Dim pointdepart = Point3d.Origin
            pointdepart = pointdepart.TransformBy(Ed.CurrentUserCoordinateSystem.Inverse)
            pointUcs = pointUcs.TransformBy(Ed.CurrentUserCoordinateSystem.Inverse)

            CoteA = pointUcs.X - pointdepart.X
            CoteB = pointUcs.Y - pointdepart.Y
            Try
                ang = Math.Atan(CoteA / CoteB)
            Catch ex As Exception
                ang = 0
            End Try

            'si Coté B négatif alors angle >180 et <0 --> rotation de 180 à faire
            If CoteB < 0 Then ang = ang - Math.PI

            'Le bloc est à l'unité cm par défaut
            Dim Unite As Double
            Dim obj As Object = acApp.GetSystemVariable("INSUNITS")
            Select Case System.Convert.ToInt16(obj)
                Case 4 : Unite = 10 'mm
                Case 5 : Unite = 1   'cm
                Case 6 : Unite = 0.01   'm
                Case Else : Unite = 1
            End Select



            BlocT = tr.GetObject(db.BlockTableId, OpenMode.ForRead)
            Dim Bloc As BlockTableRecord

            'sélection de la zone d'insertion (Objet/papier)
            '1 si objet / 0 si papier
            Dim OkEspaceObjet As Integer = acApp.GetSystemVariable("TILEMODE")
            '1 si espace papier / 2 si dans Fenetre
            Dim OkEspacePapier As Integer = acApp.GetSystemVariable("CVPORT")

            If OkEspacePapier = 1 Then
                'Espace Papier
                Bloc = tr.GetObject(BlocT.Item(BlockTableRecord.PaperSpace), OpenMode.ForWrite)
                Unite = 0.5 '(car bloc en cm et papier en mm)
            Else
                'Espace Objet ou Fenetre dans Papier
                Bloc = tr.GetObject(BlocT.Item(BlockTableRecord.ModelSpace), OpenMode.ForWrite)

            End If

            RefBloc = New BlockReference(PointDebutTableau, DefBloc.ObjectId)

            RefBloc.Layer = "0"
            RefBloc.Color = Autodesk.AutoCAD.Colors.Color.FromColorIndex(Autodesk.AutoCAD.Colors.ColorMethod.ByLayer, 256)

            RefBloc.ScaleFactors = New Scale3d(Unite, Unite, Unite)
            RefBloc.Rotation = ang

            Bloc.AppendEntity(RefBloc)

            tr.AddNewlyCreatedDBObject(RefBloc, True)


            Dim txt As String = ""
            Dim objID As ObjectId
            Dim vpIdVal As Long

            'Dim fieldEval As FieldEvaluationStatusResult = FieldObj.EvaluationStatus
            'If fieldEval.Status <> FieldEvaluationStatus.Success Then
            '    Ed.WriteMessage(vbCrLf & "FieldEvaluationStatus {0} - Message: {1}", fieldEval.Status, fieldEval.ErrorMessage)
            '    Exit Sub
            'Else

            'gestion des attributs

            Dim Index As Integer
            Dim FieldObj As Field
            Dim btAttRec As BlockTableRecord
            btAttRec = tr.GetObject(DefBloc.ObjectId, OpenMode.ForRead)
            Dim idAtt As ObjectId
            For Each idAtt In btAttRec
                Dim ent As Entity
                ent = tr.GetObject(idAtt, OpenMode.ForRead)
                If TypeOf ent Is AttributeDefinition Then
                    Dim attDef As AttributeDefinition
                    attDef = CType(ent, AttributeDefinition)
                    Dim attRef As New AttributeReference
                    attRef.SetAttributeFromBlock(attDef, RefBloc.BlockTransform)
                    'détection de l'att.
                    If attRef.Tag.Contains("NomXref") = True Then
                        Index = CInt(attRef.Tag.Replace("NomXref", ""))
                        txt = DataGridViewT.Rows.Item(Index).Cells(Enum_DataG.Handle).Value
                        objID = db.GetObjectId(False, New Handle(Int64.Parse(txt, Globalization.NumberStyles.AllowHexSpecifier)), 0)
                        vpIdVal = Convert.ToInt64(objID.ToString.Substring(1, objID.ToString.Length - 2))

                        FieldObj = New Field()
                        FieldObj.EvaluationOption = FieldEvaluationOptions.Automatic
                        FieldObj.SetFieldCode(String.Format("%<\AcObjProp Object(%<\_ObjId {0}>%).Name \f ""%tc4"">%", vpIdVal))
                        attRef.SetField(FieldObj)
                        CollObject.Add(attRef)

                    ElseIf attRef.Tag.Contains("CheminXref") = True Then
                        Index = CInt(attRef.Tag.Replace("CheminXref", ""))
                        txt = DataGridViewT.Rows.Item(Index).Cells(Enum_DataG.Handle).Value
                        objID = db.GetObjectId(False, New Handle(Int64.Parse(txt, Globalization.NumberStyles.AllowHexSpecifier)), 0)

                        vpIdVal = Convert.ToInt64(objID.ToString.Substring(1, objID.ToString.Length - 2))

                        FieldObj = New Field()
                        FieldObj.EvaluationOption = FieldEvaluationOptions.Automatic

                        If RadioButtonNomFichierPath.Checked = True Then
                            'Nom + Fichier
                            FieldObj.SetFieldCode(String.Format("%<\AcObjProp Object(%<\_ObjId {0}>%,1).Path \f ""%tc4"">%", vpIdVal))
                        Else
                            'Nom Fichier uniquement
                            FieldObj.SetFieldCode(String.Format("%<\AcObjProp Object(%<\_ObjId {0}>%,1).Path \f ""%fn6"">%", vpIdVal))
                        End If


                        attRef.SetField(FieldObj)
                        CollObject.Add(attRef)
                    End If

                    RefBloc.AttributeCollection.AppendAttribute(attRef)
                    tr.AddNewlyCreatedDBObject(attRef, True)
                End If
            Next


        Catch ex As Exception
            MsgBox("Erreur à l'insertion du bloc", MsgBoxStyle.Critical & vbOKOnly, "Erreur système dans ClassFichier.GestionBlocTableau")
        Finally
            tr.Commit()
            tr.Dispose()
        End Try
        '-->V2.0.0.1
        'Regen pour générer le remplissage des champs.
#If _AcadVer_ = 2010 Then
        GestionAUTOCAD.LigneCommandeImmediate("_regenall")
#Else
        acApp.DocumentManager.MdiActiveDocument.Editor.Regen()
#End If
        '<--V2.0.0.1

 

here a sample code for inserting a blocreference and set a attribut with à field.

te attribut show the name of a Xref inserted in the dwg.

 

Sorry i am french so my annotation is in my language.

 

You cant copy/past this code in your vb app. but you cade use the part of the use of field.

 

++

Message 3 of 4
dennis
in reply to: AubelecBE

Thanks AubelecBE, I will take a look at this today and see what I can do.  At first glance, you have pointed to an attribute changed to a field, while I am after the field of the drawing itself, much like maybe going after the Sheet Set Manager field.  But nonetheless, maybe I can make the jump over.

Message 4 of 4
philippe.leefsma
in reply to: dennis

Check out my post, it illustrates how to find all fields in a drawing:

 

http://adndevblog.typepad.com/autocad/2014/07/how-to-find-all-fields-in-a-drawing.html

 

Philippe.



Philippe Leefsma
Developer Technical Services
Autodesk Developer Network

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