Recent Posts

Pages: 1 ... 5 6 [7] 8 9 10
61
Blocks / Change text object in block reference
« Last post by fixo on March 15, 2013, 04:57:16 PM »
   C#
Code: [Select]
     
        // Change the text object in the BlockTableRecord by selecting them on screen
        // This method may have any name
        [CommandMethod("cato", CommandFlags.UsePickSet | CommandFlags.Redraw)]
        public static void ChangeTextInBlockDef()
        {
            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;

            Database db = doc.Database;

            Editor ed = doc.Editor;

            Transaction tr = db.TransactionManager.StartTransaction();
            using (tr)
            {
                doc.TransactionManager.EnableGraphicsFlush(true);
                PromptNestedEntityOptions pno = new PromptNestedEntityOptions("\nSelect text within block instance: ");
                pno.AllowNone = true;
                pno.UseNonInteractivePickPoint = false;
                PromptNestedEntityResult rs = ed.GetNestedEntity(pno);
                if (rs.Status != PromptStatus.OK) return;
                Entity selent = (Entity)tr.GetObject(rs.ObjectId, OpenMode.ForRead);
                ed.WriteMessage("\nSelected type of {0}", selent.GetType().Name);

                PromptStringOptions pso = new PromptStringOptions("\nEnter new text: ");
                pso.AllowSpaces = true;
                PromptResult res;
                res = ed.GetString(pso);
                if (res.Status != PromptStatus.OK) return;
                string newstr = res.StringResult;
                ed.WriteMessage("\nNew Text Entered:\t{0}", newstr);

                BlockTableRecord btrec = tr.GetObject(selent.OwnerId, OpenMode.ForWrite) as BlockTableRecord;
                // prevent work with attributes and embedded blocks
                if (selent.ObjectId.ObjectClass.IsDerivedFrom(RXClass.GetClass(typeof(AttributeReference))) |
                    selent.ObjectId.ObjectClass.IsDerivedFrom(RXClass.GetClass(typeof(BlockReference)))) return;
                if (selent.ObjectId.ObjectClass.IsDerivedFrom(RXClass.GetClass(typeof(DBText))))
                {
                    DBText txt = selent as DBText;
                    string oldstr = txt.TextString;

                    BlockTableRecord owner = (BlockTableRecord)tr.GetObject(txt.OwnerId, OpenMode.ForRead);
                    ed.WriteMessage("\nName:\t{0}", owner.Name);
                    owner.UpgradeOpen();
                    foreach (ObjectId id in owner)
                    {
                        if (!id.ObjectClass.IsDerivedFrom(RXClass.GetClass(typeof(DBText)))) continue;
                        DBObject obj = tr.GetObject(id, OpenMode.ForRead);
                        DBText blktxt = obj as DBText;
                        if (blktxt.TextString != oldstr) continue;
                        blktxt.UpgradeOpen();
                        blktxt.TextString = newstr;

                    }

                    //--------------------------------------------------------------
                    // add other possible types such as mtext, dimension etc here
                    //--------------------------------------------------------------


                    tr.TransactionManager.QueueForGraphicsFlush();

                    doc.TransactionManager.FlushGraphics();

                    tr.Commit();

                    ed.Regen();
                }
            }
        }

   VB.NET
Code: [Select]
        ' Change the text object in the BlockTableRecord by selecting them on screen
        ' This method may have any name
        <CommandMethod("cato", CommandFlags.UsePickSet Or CommandFlags.Redraw)> _
        Public Shared Sub ChangeTextInBlockDef()
            Dim doc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument

            Dim db As Database = doc.Database

            Dim ed As Editor = doc.Editor

            Dim tr As Transaction = db.TransactionManager.StartTransaction()
            Using tr
                doc.TransactionManager.EnableGraphicsFlush(True)
                Dim pno As New PromptNestedEntityOptions(vbLf & "Select text within block instance: ")
                pno.AllowNone = True
                pno.UseNonInteractivePickPoint = False
                Dim rs As PromptNestedEntityResult = ed.GetNestedEntity(pno)
                If rs.Status <> PromptStatus.OK Then
                    Return
                End If
                Dim selent As Entity = DirectCast(tr.GetObject(rs.ObjectId, OpenMode.ForRead), Entity)
                ed.WriteMessage(vbLf & "Selected type of {0}", selent.[GetType]().Name)

                Dim pso As New PromptStringOptions(vbLf & "Enter new text: ")
                pso.AllowSpaces = True
                Dim res As PromptResult
                res = ed.GetString(pso)
                If res.Status <> PromptStatus.OK Then
                    Return
                End If
                Dim newstr As String = res.StringResult
                ed.WriteMessage(vbLf & "New Text Entered:" & vbTab & "{0}", newstr)

                Dim btrec As BlockTableRecord = TryCast(tr.GetObject(selent.OwnerId, OpenMode.ForWrite), BlockTableRecord)
                ' prevent work with attributes and embedded blocks
                If selent.ObjectId.ObjectClass.IsDerivedFrom(RXClass.GetClass(GetType(AttributeReference))) Or selent.ObjectId.ObjectClass.IsDerivedFrom(RXClass.GetClass(GetType(BlockReference))) Then
                    Return
                End If
                If selent.ObjectId.ObjectClass.IsDerivedFrom(RXClass.GetClass(GetType(DBText))) Then
                    Dim txt As DBText = TryCast(selent, DBText)
                    Dim oldstr As String = txt.TextString

                    Dim owner As BlockTableRecord = DirectCast(tr.GetObject(txt.OwnerId, OpenMode.ForRead), BlockTableRecord)
                    ed.WriteMessage(vbLf & "Name:" & vbTab & "{0}", owner.Name)
                    owner.UpgradeOpen()
                    For Each id As ObjectId In owner
                        If Not id.ObjectClass.IsDerivedFrom(RXClass.GetClass(GetType(DBText))) Then
                            Continue For
                        End If
                        Dim obj As DBObject = tr.GetObject(id, OpenMode.ForRead)
                        Dim blktxt As DBText = TryCast(obj, DBText)
                        If blktxt.TextString <> oldstr Then
                            Continue For
                        End If
                        blktxt.UpgradeOpen()

                        blktxt.TextString = newstr
                    Next

                    '--------------------------------------------------------------
                    ' add other possible types such as mtext, dimension etc here
                    '--------------------------------------------------------------


                    tr.TransactionManager.QueueForGraphicsFlush()

                    doc.TransactionManager.FlushGraphics()

                    tr.Commit()

                    ed.Regen()
                End If
            End Using
        End Sub
62
Dimensions / Re: Some basic dimensions function
« Last post by fixo on March 14, 2013, 03:50:40 PM »
Can you upload small picture (.png or .jpg) to see what
you exacrly need?
63
Dimensions / Re: Some basic dimensions function
« Last post by dockee on March 13, 2013, 10:33:40 PM »
hi,

I manage to create an aligneddim but I have one parameter to put this last in a grid iso autocad.

Can you help me.

Thanks.
64
Text / Place Text to center using calculated text width
« Last post by fixo on March 12, 2013, 05:52:23 PM »
   C# 
       
Code: [Select]
   
       // Place Text to center using calculated text width
        // tested on A2010 .NET Framework 3.5
        [CommandMethod("tcenter")]
        public static void textToCenter()
        {
            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Autodesk.AutoCAD.GraphicsInterface.TextStyle style = new Autodesk.AutoCAD.GraphicsInterface.TextStyle();
            byte n;

            Transaction tr = db.TransactionManager.StartTransaction();
            try
            {
                using (tr)
                {
                    BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
                    // setup the text
                    string text = "1234567890";
                    // add new dbtext to current space
                    DBText txt = new DBText();
                    txt.SetDatabaseDefaults();
                    txt.TextString = text;
                    txt.Position = new Point3d(100, 200, 0);
                    btr.AppendEntity(txt);
                    tr.AddNewlyCreatedDBObject(txt, true);
                    // get textstyle of newly created text
                    TextStyleTableRecord txtbtr = (TextStyleTableRecord)tr.GetObject(txt.TextStyleId, OpenMode.ForRead);
                    // copy properties from TextStyleTableRecord and dbtext to temp AcGi.TextStyle (just very limited one for the future calculation)
                    style.FileName = txtbtr.FileName;
                    // then copy properties from existing text
                    style.TextSize = txt.Height;  // txtbtr.TextSize;
                    style.ObliquingAngle = txt.Oblique;
                    style.XScale = txt.WidthFactor;
                    // load temp style record
                    try
                    {
                        n = style.LoadStyleRec;
                    }
                    catch { return; }// something wrong then exit on error

                    // set new position of text center, i.e. some dummy point
                    Point3d cpt = new Point3d(20, -45, 0);

                    // find out the extents
                    Point2d minpt, maxpt;
                    // get extends of text
                    Extents2d ex = style.ExtentsBox(text, true, true, null);

                    minpt = ex.MinPoint;
                    maxpt = ex.MaxPoint;
                    // work out the insertion point
                    Point3d newpos = cpt - new Vector3d((minpt.X + maxpt.X) / 2.0, (minpt.Y + maxpt.Y) / 2.0, 0);
                    // change text position to be centered in this point, independently of text alignment mode
                    txt.Position = newpos;
                    style.Dispose();// it's not a database resident, so dispose style, optional
                    tr.Commit();
                }
            }
            catch (System.Exception exc)
            {
                Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog(exc.Message + "\n" + exc.StackTrace);
            }
            finally { }
        }

   VB.NET

Code: [Select]
      ' Place Text to center using calculated text width
        ' tested on A2010 .NET Framework 3.5
        <CommandMethod("tcenter")> _
        Public Shared Sub textToCenter()
            Dim doc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
            Dim db As Database = doc.Database
            Dim style As New Autodesk.AutoCAD.GraphicsInterface.TextStyle()
            Dim n As Byte

            Dim tr As Transaction = db.TransactionManager.StartTransaction()
            Try
                Using tr
                    Dim btr As BlockTableRecord = DirectCast(tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite), BlockTableRecord)
                    ' setup the text
                    Dim text As String = "1234567890"
                    ' add new dbtext to current space
                    Dim txt As New DBText()
                    txt.SetDatabaseDefaults()
                    txt.TextString = text
                    txt.Position = New Point3d(100, 200, 0)
                    btr.AppendEntity(txt)
                    tr.AddNewlyCreatedDBObject(txt, True)
                    ' get textstyle of newly created text
                    Dim txtbtr As TextStyleTableRecord = DirectCast(tr.GetObject(txt.TextStyleId, OpenMode.ForRead), TextStyleTableRecord)
                    ' copy properties from TextStyleTableRecord and dbtext to temp AcGi.TextStyle (just very limited one for the future calculation)
                    style.FileName = txtbtr.FileName
                    ' then copy properties from existing text
                    style.TextSize = txt.Height
                    ' txtbtr.TextSize;
                    style.ObliquingAngle = txt.Oblique
                    style.XScale = txt.WidthFactor
                    ' load temp style record
                    Try
                        n = style.LoadStyleRec
                    Catch
                        Return
                    End Try
                    ' something wrong then exit on error
                    ' set new position of text center, i.e. some dummy point
                    Dim cpt As New Point3d(20, -45, 0)

                    ' find out the extents
                    Dim minpt As Point2d, maxpt As Point2d
                    ' get extends of text
                    Dim ex As Extents2d = style.ExtentsBox(text, True, True, Nothing)

                    minpt = ex.MinPoint
                    maxpt = ex.MaxPoint
                    ' work out the insertion point
                    Dim newpos As Point3d = cpt - New Vector3d((minpt.X + maxpt.X) / 2.0, (minpt.Y + maxpt.Y) / 2.0, 0)
                    ' change text position to be centered in this point, independently of text alignment mode
                    txt.Position = newpos
                    style.Dispose()
                    ' it's not a database resident, so dispose style, optional
                    tr.Commit()
                End Using
            Catch exc As System.Exception
                Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog(exc.Message & vbLf & exc.StackTrace)
            Finally
            End Try
        End Sub
65
Dimensions / Draw runned dimensions for panel holes
« Last post by fixo on March 11, 2013, 10:15:46 PM »
For horizontal dimensions the first point is on left or right edge of panel,
for vertical dimensions it would be on top or bottom edge of panel for your choice

Code: [Select]
#Region "Runned Dimensions"
        <CommandMethod("rundim")> _
        Public Sub testRunnedDimensions()
            Dim doc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
            Dim db As Database = doc.Database
            Dim ed As Editor = doc.Editor
            Dim ucs As Matrix3d = ed.CurrentUserCoordinateSystem
            Dim pko As New PromptKeywordOptions(vbLf & "Choose Dimension direction [Horizontal/Vertical]: ", "Horizontal Vertical")
            ' The default depends on our current settings
            pko.Keywords.Default = "Horizontal"
            Dim pres As PromptResult = ed.GetKeywords(pko)
            If pres.Status <> PromptStatus.OK Then
                Return
            End If
            Dim choice As String = pres.StringResult

            Dim spt As New Point3d
            Autodesk.AutoCAD.ApplicationServices.Application.SetSystemVariable("osmode", 513)
            If Not GetFirstPoint(ed, "Pick first point (press Enter to Exit loop): ", spt) Then Return
            Dim oids() As ObjectId = GetUserPickedObjects(doc, "Select circles: ")
            ed.Regen()
            doc.TransactionManager.EnableGraphicsFlush(True)
            Dim up As Double = db.Dimtxt * 4
            Using tr As Transaction = doc.Database.TransactionManager.StartTransaction()

                Dim btr As BlockTableRecord = CType(tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite), BlockTableRecord)
                Dim cnt As Integer = 0
                Dim ang As Double
                Dim rdim As RotatedDimension = Nothing
                For Each id As ObjectId In oids
                    If (Not id.ObjectClass.IsDerivedFrom(RXClass.GetClass(GetType(Circle)))) Then Continue For
                    Dim ent As DBObject = tr.GetObject(id, OpenMode.ForRead, False)
                    Dim circ As Circle = TryCast(ent, Circle)
                    If circ Is Nothing Then Return
                    Dim cp As Point3d = circ.Center
                    Dim gap As Double = cp.X - spt.X
                    If cnt = 0 Then
                        up = circ.Radius + up
                        If choice = "Horizontal" Then
                            spt = New Point3d(spt.X, cp.Y, spt.Z)
                        Else
                            spt = New Point3d(cp.X, spt.Y, spt.Z)
                        End If
                        cnt += 1
                    End If
                    Dim txp As Point3d
                    If choice = "Horizontal" Then
                        ang = 0.0
                        txp = spt.Add(New Vector3d(0, up, 0))
                        rdim = New RotatedDimension(ang, spt, cp, txp, "", db.Dimstyle)
                        Dim hdist As Double
                        Dim hpt As Point3d
                        If spt.X < cp.X Then
                            hdist = rdim.Measurement - db.Dimtxt * 6
                        Else
                            hdist = -rdim.Measurement + db.Dimtxt * 6
                        End If
                        hpt = txp.Add(New Vector3d(hdist, 0, 0))
                        rdim.Dimsd1 = True
                        rdim.Dimse1 = True
                        rdim.TextPosition = hpt
                    Else
                        ang = Math.PI / 2
                        txp = spt.Add(New Vector3d(up, 0, 0))
                        rdim = New RotatedDimension(ang, spt, cp, txp, "", db.Dimstyle)
                        Dim vdist As Double
                        Dim vpt As Point3d
                        If spt.Y < cp.Y Then
                            vdist = rdim.Measurement - db.Dimtxt * 6
                        Else
                            vdist = -rdim.Measurement + db.Dimtxt * 6
                        End If
                        vpt = txp.Add(New Vector3d(0, vdist, 0))
                        rdim.Dimsd1 = True
                        rdim.Dimse1 = True
                        rdim.TextPosition = vpt
                        rdim.TextRotation = ang
                    End If

                    btr.AppendEntity(rdim)
                    tr.AddNewlyCreatedDBObject(rdim, True)
                    tr.TransactionManager.QueueForGraphicsFlush()
                Next
                doc.TransactionManager.FlushGraphics()
                tr.Commit()
                ed.UpdateScreen()
            End Using
        End Sub
 


        Private Shared Function GetUserPickedObjects(ByVal doc As Document, msg As String) As ObjectId()
            Dim ids As New List(Of ObjectId)
            Using tr As Transaction = doc.Database.TransactionManager.StartTransaction()
                Dim go As Boolean = True
                While go
                    go = False

                    Dim opt As New PromptEntityOptions(vbLf & msg)
                    Dim res As PromptEntityResult = doc.Editor.GetEntity(opt)

                    If res.Status = PromptStatus.OK Then
                        Dim exists As Boolean = False
                        For Each id As ObjectId In ids
                            If id = res.ObjectId Then
                                exists = True
                                Exit For
                            End If
                        Next

                        If Not exists Then
                            'Highlight
                            Dim ent As Entity = DirectCast(tr.GetObject(res.ObjectId, OpenMode.ForWrite), Entity)

                            ent.Highlight()

                            ids.Add(res.ObjectId)
                            go = True
                        End If
                    End If
                End While

                tr.Commit()
            End Using

            Return ids.ToArray()
        End Function
#End Region
66
Dimensions / Draw stacked dimensions for panel holes
« Last post by fixo on March 11, 2013, 08:13:12 PM »
For horizontal dimensions the first point is on left or right edge of panel,
for vertical dimensions it would be on top or bottom edge of panel for your choice

Code: [Select]
       #Region "Stacked Dimension"
        <CommandMethod("stackdim")> _
        Public Sub testDimStackHoles()
            Dim doc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
            Dim db As Database = doc.Database
            Dim ed As Editor = doc.Editor
            Dim pko As New PromptKeywordOptions(vbLf & "Choose Dimension direction [Horizontal/Vertical]: ", "Horizontal Vertical")
            ' The default depends on our current settings
            pko.Keywords.Default = "Horizontal"
            Dim pres As PromptResult = ed.GetKeywords(pko)
            If pres.Status <> PromptStatus.OK Then
                Return
            End If
            Dim choice As String = pres.StringResult

            Dim spt As New Point3d
            Autodesk.AutoCAD.ApplicationServices.Application.SetSystemVariable("osmode", 513)
            If Not GetFirstPoint(ed, "Pick first point (press Enter to Exit loop): ", spt) Then Return
            Dim oids() As ObjectId = GetUserPickedObjects(doc, "Select circles: ")
            ed.Regen()
            doc.TransactionManager.EnableGraphicsFlush(True)
            Dim up As Double = db.Dimtxt * 4
            Using tr As Transaction = doc.Database.TransactionManager.StartTransaction()

                Dim btr As BlockTableRecord = CType(tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite), BlockTableRecord)
                Dim cnt As Integer = 0
                Dim ang As Double
                Dim rdim As RotatedDimension = Nothing
                For Each id As ObjectId In oids
                    If (Not id.ObjectClass.IsDerivedFrom(RXClass.GetClass(GetType(Circle)))) Then Continue For
                    Dim ent As DBObject = tr.GetObject(id, OpenMode.ForRead, False)
                    Dim circ As Circle = TryCast(ent, Circle)
                    If circ Is Nothing Then Return
                    Dim cp As Point3d = circ.Center
                    Dim gap As Double = cp.X - spt.X
                    If cnt = 0 Then
                        up = circ.Radius + up
                        If choice = "Horizontal" Then
                            spt = New Point3d(spt.X, cp.Y, spt.Z)
                        Else
                            spt = New Point3d(cp.X, spt.Y, spt.Z)
                        End If
                        cnt += 1
                    End If
                    If choice = "Horizontal" Then
                        ang = 0.0
                        rdim = New RotatedDimension(ang, spt, cp, spt + New Vector3d(0, up, 0), "", db.Dimstyle)
                    Else
                        ang = Math.PI / 2
                        rdim = New RotatedDimension(ang, spt, cp, spt - New Vector3d(up, 0, 0), "", db.Dimstyle)
                    End If
                    up += db.Dimtxt * 2
                    btr.AppendEntity(rdim)
                    tr.AddNewlyCreatedDBObject(rdim, True)
                    tr.TransactionManager.QueueForGraphicsFlush()
                Next
                doc.TransactionManager.FlushGraphics()
                tr.Commit()
                ed.UpdateScreen()
            End Using
        End Sub


        Private Shared Function GetUserPickedObjects(ByVal doc As Document, msg As String) As ObjectId()
            Dim ids As New List(Of ObjectId)
            Using tr As Transaction = doc.Database.TransactionManager.StartTransaction()
                Dim go As Boolean = True
                While go
                    go = False

                    Dim opt As New PromptEntityOptions(vbLf & msg)
                    Dim res As PromptEntityResult = doc.Editor.GetEntity(opt)

                    If res.Status = PromptStatus.OK Then
                        Dim exists As Boolean = False
                        For Each id As ObjectId In ids
                            If id = res.ObjectId Then
                                exists = True
                                Exit For
                            End If
                        Next

                        If Not exists Then
                            'Highlight
                            Dim ent As Entity = DirectCast(tr.GetObject(res.ObjectId, OpenMode.ForWrite), Entity)

                            ent.Highlight()

                            ids.Add(res.ObjectId)
                            go = True
                        End If
                    End If
                End While

                tr.Commit()
            End Using

            Return ids.ToArray()
        End Function
#End Region
67
This site / Re: Welcome on the AutoCAD .NET developpers forums
« Last post by dockee on March 11, 2013, 07:56:04 PM »
Hi my name is fabrice.

Thank  for this site.

68
Windows forms / Use dinamically created form in the project
« Last post by fixo on March 11, 2013, 07:11:42 PM »
I've created simple project to show the way
how to use dinamic form for user interaction
Please see attached .zip file and read HowTo.txt for more
70
Layouts and printing / Set UCS in a viewport in paperspace
« Last post by sergdgt on March 05, 2013, 01:26:39 PM »
Hello I have the following problem. I'm in model space, create some "myUCS". Then create a new paper space, and in it the viewport. I can not set the viewport "myUCS". Maybe you have had a similar case or an example.

Please help.
Pages: 1 ... 5 6 [7] 8 9 10