Recent Posts

Pages: 1 ... 5 6 [7] 8 9 10
61
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?
62
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.
63
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
64
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
65
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
66
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.

67
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
69
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.
70
Text / Create Mtext using Notepad for input string (A2010)
« Last post by fixo on March 04, 2013, 10:00:47 AM »
     C#
Code: [Select]
        [System.Security.SuppressUnmanagedCodeSecurity]
        [DllImport("acad.exe", EntryPoint = "acedCmd", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
        extern static private int acedCmd(IntPtr resbuf);
        //-----------------------------------------------------------//
        // AutoCAD 2010 (eng.)
        [CommandMethod("mtx")]
        public void createMtext()
        {
            Clipboard.Clear();
            Document doc = doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;

            Editor ed = doc.Editor;
            Database db = doc.Database;
            Transaction tr = db.TransactionManager.StartTransaction();
            Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog("Add then \"Cut\" text from Notepad");
            System.Threading.Thread.Sleep(500);

            ResultBuffer rb = new ResultBuffer();

            rb.Add(new TypedValue((int)LispDataType.Text, "_startapp"));
            rb.Add(new TypedValue((int)LispDataType.Text, "notepad"));
            rb.Add(new TypedValue((int)LispDataType.Text, ""));// open Unnamed.txt
            rb.Add(new TypedValue((int)LispDataType.Text, "\n"));
            acedCmd(rb.UnmanagedObject);
            ed.Regen();
            using (tr)
            {   
                Point3d pt = ed.GetPoint("\nPick mtext location: ").Value;
                MText mtx = new MText();
                mtx.Location = pt;
                mtx.SetDatabaseDefaults();
                mtx.TextStyleId = db.Textstyle;// current text size
                mtx.TextHeight = db.Dimtxt;// current textstyle
                mtx.Width = 0.0;
                mtx.Contents = Clipboard.GetText();// paste mtext contents from clipboard
                // set alignment wharever you need:
                mtx.Attachment = AttachmentPoint.BottomCenter;
                mtx.SetAttachmentMovingLocation(mtx.Attachment);
                BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
                btr.AppendEntity(mtx);
                tr.AddNewlyCreatedDBObject(mtx, true);
                mtx.Width = mtx.ActualWidth;
                tr.Commit();
            }
           // Clipboard.Clear();// optional, if you won't use it then
        }

   VB.NET

   
Code: [Select]
  <System.Security.SuppressUnmanagedCodeSecurity()> _
<DllImport("acad.exe", EntryPoint:="acedCmd", CharSet:=CharSet.Unicode, CallingConvention:=CallingConvention.Cdecl)> _
        Private Shared Function acedCmd(resbuf As IntPtr) As Integer
        End Function
        '-----------------------------------------------------------//
        ' AutoCAD 2010 (32bit eng.) Win7
        <CommandMethod("mtx")> _
        Public Sub createMtext()
            System.Windows.Clipboard.Clear()
            Dim doc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument

            Dim ed As Editor = doc.Editor
            Dim db As Database = doc.Database
            Dim tr As Transaction = db.TransactionManager.StartTransaction()
            Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog("Add then ""Cut"" text from Notepad")
            System.Threading.Thread.Sleep(500)

            Dim rb As New ResultBuffer()

            rb.Add(New TypedValue(CInt(LispDataType.Text), "_startapp"))
            rb.Add(New TypedValue(CInt(LispDataType.Text), "notepad"))
            rb.Add(New TypedValue(CInt(LispDataType.Text), ""))
            ' open file Unnamed.txt:
            rb.Add(New TypedValue(CInt(LispDataType.Text), vbLf))
            acedCmd(rb.UnmanagedObject)
            ed.Regen()
            Using tr
                Dim pt As Point3d = ed.GetPoint(vbLf & "Pick mtext location: ").Value
                Dim mtx As New MText()
                mtx.Location = pt
                mtx.SetDatabaseDefaults()
                mtx.TextStyleId = db.Textstyle
                ' current text size
                mtx.TextHeight = db.Dimtxt
                ' current textstyle
                mtx.Width = 0.0
                ' paste mtext contents from clipboard
                mtx.Contents = System.Windows.Clipboard.GetText()
                ' set alignment wharever you need:
                mtx.Attachment = AttachmentPoint.BottomCenter
                mtx.SetAttachmentMovingLocation(mtx.Attachment)
                Dim btr As BlockTableRecord = DirectCast(tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite), BlockTableRecord)
                btr.AppendEntity(mtx)
                tr.AddNewlyCreatedDBObject(mtx, True)
                mtx.Width = mtx.ActualWidth
                tr.Commit()
            End Using
            ' System.Windows.Clipboard.Clear();'' optional, if you won't use it then
        End Sub
Pages: 1 ... 5 6 [7] 8 9 10