Author Topic: create Mtext in the midpoint of a rectangle  (Read 13268 times)

0 Members and 1 Guest are viewing this topic.

Fatty

  • Guest
Re: create Mtext in the midpoint of a rectangle
« Reply #15 on: May 30, 2007, 03:08:54 PM »
Mathimatically this is very problematic.
The first instinct is to use the centroid as Matt suggested,
however this fails with an "L" as the centroid is outside the line,
Luis's method will always work (if I can read it correctly)
Thanks Bryco I knew that by OP
asked about contours similar on
rectangles though...
Regards

~'J'~

LE

  • Guest
Re: create Mtext in the midpoint of a rectangle
« Reply #16 on: May 30, 2007, 04:00:47 PM »
Mathimatically this is very problematic.
The first instinct is to use the centroid as Matt suggested,
however this fails with an "L" as the centroid is outside the line,
Luis's method will always work (if I can read it correctly)

Bryco;

I did not look at that, but to fix it in a simple way is to just add the code below to my function:

Code: [Select]
Point3d p1 = poly.GetPointAtParameter(0.0);
Line line1 = new Line(p1, geoCtr);

Point3dCollection points = new Point3dCollection();
poly.IntersectWith(line1, Intersect.OnBothOperands, points, 0, 0);

if (points.Count == 2)
{
    txt.AlignmentPoint = Polar(points[0], Angle(points[0], points[1]), Distance(points[0], points[1]) / 2.0);
}

The above, will force the position of the text to be inside of the area, if it is an L or U shape, is not optimized but (can be) works!

And the whole function modified/updated:
Code: [Select]
[CommandMethod("PLACETEXT")]//place some text inside a rectangle area
public void placetext()
{
    Document doc = acadApp.DocumentManager.MdiActiveDocument;
    Editor ed = doc.Editor;
    Database db = doc.Database;
    PromptEntityResult res = ed.GetEntity("\nSelect the rectangle base line: ");
    if (res.Status != PromptStatus.OK) return;
    using (Transaction tr = db.TransactionManager.StartTransaction())
    {
        Polyline poly = tr.GetObject(res.ObjectId, OpenMode.ForRead, false) as Polyline;
        if (poly != null)
        {
            Point3d pickPoint = res.PickedPoint;
            Point3d oPoint = poly.GetClosestPointTo(pickPoint, ed.GetCurrentView().ViewDirection, false);
            double param = 0;
            param = poly.GetParameterAtPoint(oPoint);
            double sparam=0, eparam=0;
            sparam = (int)param;
            eparam = sparam + 1;
            Point3d sp = poly.GetPointAtParameter(sparam);
            Point3d ep = poly.GetPointAtParameter(eparam);
            double ang = Angle(sp, ep);
            Extents3d ext = poly.GeometricExtents;
            Point3d min = ext.MinPoint;
            Point3d max = ext.MaxPoint;
            Point3d geoCtr = Polar(min, Angle(min, max), Distance(min, max) / 2.0);
            BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
            DBText txt = new DBText();
            txt.TextString = "TESTING";
            txt.SetDatabaseDefaults(db);
            txt.Height = Distance(min, max) / 8.0;
            txt.HorizontalMode = TextHorizontalMode.TextMid;
            txt.Rotation = ang;
            btr.AppendEntity(txt);
            tr.AddNewlyCreatedDBObject(txt, true);
            txt.AlignmentPoint = geoCtr;

            Point3d p1 = poly.GetPointAtParameter(0.0);
            Line line1 = new Line(p1, geoCtr);

            Point3dCollection points = new Point3dCollection();
            poly.IntersectWith(line1, Intersect.OnBothOperands, points, 0, 0);

            if (points.Count == 2)
            {
                txt.AlignmentPoint = Polar(points[0], Angle(points[0], points[1]), Distance(points[0], points[1]) / 2.0);
            }
        }
        tr.Commit();
    }
}

Bryco

  • Water Moccasin
  • Posts: 1882
Re: create Mtext in the midpoint of a rectangle
« Reply #17 on: May 30, 2007, 04:09:10 PM »
I'll try that tonight Luis.
Fatty you are right.

Humbertogo

  • Guest
Re: create Mtext in the midpoint of a rectangle
« Reply #18 on: May 31, 2007, 12:38:26 PM »
I will try in C#
one more question how can i translatethis code  in C#
 :-D
Code: [Select]

              .SendCommand Chr(3) & Chr(3) & "._-boundary" & vbCr & pstr & vbCr & vbCr
             
                  Set LastObj = .ModelSpace.Item(.ModelSpace.Count - 1)
            If TypeOf LastObj Is AcadLWPolyline Then
                  Set objLWPolyline(0) = LastObj
                      objLWPolyline(0).GetBoundingBox varMinPt, varMaxPt
                      objLWPolyline(0).Delete

              End If

         

LE

  • Guest
Re: create Mtext in the midpoint of a rectangle
« Reply #19 on: May 31, 2007, 12:58:38 PM »
I will try in C#
one more question how can i translatethis code  in C#
 :-D
Code: [Select]

              .SendCommand Chr(3) & Chr(3) & "._-boundary" & vbCr & pstr & vbCr & vbCr
             
                  Set LastObj = .ModelSpace.Item(.ModelSpace.Count - 1)
            If TypeOf LastObj Is AcadLWPolyline Then
                  Set objLWPolyline(0) = LastObj
                      objLWPolyline(0).GetBoundingBox varMinPt, varMaxPt
                      objLWPolyline(0).Delete

              End If

         

 :-o want to use sendcommand?... I would rather go and draw the polyline without using the command boundary... but is up to you.

If I have time, I will try to write something, so you pick the points of an area, then create a closed polyline, and want to place any text inside no? or is that text the area?... just want to confirm....

Thanks.

Humbertogo

  • Guest
Re: create Mtext in the midpoint of a rectangle
« Reply #20 on: May 31, 2007, 01:22:38 PM »
LE,

In my drawing i have a routing of house, my idea was to pick
a point in mid. of each house create a closed polyline and place  text inside


LE

  • Guest
Re: create Mtext in the midpoint of a rectangle
« Reply #21 on: May 31, 2007, 01:47:54 PM »
I see, ... but you have here something to start (did you tested the command PLACETEXT or adapted the C# code to suit your needs?), the only other problem I see, is that when you end up having an area, where the command boundary or bpoly, not capable to generate the closed area, then you will require to do it manually or make your own boundary generator, when bpoly fails,  will see if a solution is provided...

[edited by me]
« Last Edit: June 04, 2007, 09:54:14 AM by LE »

Humbertogo

  • Guest
Re: create Mtext in the midpoint of a rectangle
« Reply #22 on: May 31, 2007, 03:09:05 PM »
Oh y that is the one :-D

havano

  • Guest
Re: create Mtext in the midpoint of a rectangle
« Reply #23 on: June 03, 2007, 10:26:33 AM »
I solved your problem in VBA by not using Mtext but linear Dimensions. First, I determined the midpoint XYs of the two opposite vertical(ish) rectangle sides for positioning and aligning the dimension. Then, I set the dimension offset to 0, turned all dimension graphics off (arrows etc.) and replaced the dimension value with the text I desired.
« Last Edit: June 03, 2007, 10:47:52 AM by havano »

Matersammichman

  • Guest
Re: create Mtext in the midpoint of a rectangle
« Reply #24 on: June 04, 2007, 09:02:46 AM »
I'm trying to do something along those same lines- :lol:
I'm working at using a Region to obtain the Room Length x Width for a Room Size attribute.
Anyone else already done this...?

Humbertogo

  • Guest
Re: create Mtext in the midpoint of a rectangle
« Reply #25 on: June 05, 2007, 04:41:11 AM »
may no the best way but i solved the  problem in VBA  :-D

Code: [Select]

Public Sub test()
Dim intnum As Integer
Dim PolyCoord As Variant
Dim PolySp(0 To 2) As Double
Dim PolyEp(0 To 2) As Double
Dim AngleInDegree As Double
Dim strAng  As String

intnum = 5
NumCout = 2


     With ThisDrawing

          .SetVariable "OSMODE", 0
          .SetVariable "CMDECHO", 0

         
          Msg = vbCrLf & "Select an Internal Point"

          Do
               On Error Resume Next
               Pt = .Utility.GetPoint(, Msg)
               If Err Then
                    Err.Clear
                    Exit Do
               End If
               On Error GoTo 0

               pstr = Replace(CStr(Pt(0)), ",", ".") & "," & _
                      Replace(CStr(Pt(1)), ",", ".")

               .SendCommand Chr(3) & Chr(3) & "._-boundary" & vbCr & pstr & vbCr & vbCr
             
                  Set LastObj = .ModelSpace.Item(.ModelSpace.Count - 1)
            If TypeOf LastObj Is AcadLWPolyline Then
                  Set objLWPolyline(0) = LastObj
                      objLWPolyline(0).GetBoundingBox varMinPt, varMaxPt
                ' Return all the coordinates of the polyline
                      PolyCoord = objLWPolyline(0).Coordinates
                      PolySp(0) = PolyCoord(0): PolySp(1) = PolyCoord(1)
                      PolyEp(0) = PolyCoord(2): PolyEp(1) = PolyCoord(3)
                     
                       objLWPolyline(0).Delete
                     
                Set objLine = ThisDrawing.ModelSpace.AddLine(PolySp, PolyEp)
                    AngleInDegree = Format(objLine.Angle / PI * 180# + 90 - 270, "##.000")
                    objLine.Delete
                    strAng = Replace(CStr(AngleInDegree), ",", ".")
                    dblAngle = .Utility.AngleToReal(strAng, acDegrees)
                           
              End If

               corner(0) = varMinPt(0): corner(1) = varMaxPt(1): corner(2) = 0#

                Height = 2000#
                midPoint(0) = (varMinPt(0) + varMaxPt(0)) / 2
                midPoint(1) = (varMinPt(1) + varMaxPt(1)) / 2
                midPoint(2) = 0
               
               Set MTextObj = .ModelSpace.AddMText(corner, 10, " {\fVerdana|b0|i0|c0|p34;" & intnum & "}")
                   MTextObj.Height = Height
                   MTextObj.AttachmentPoint = acAttachmentPointMiddleCenter
             Call MTextObj.Rotate(midPoint, dblAngle)
                   MTextObj.Move MTextObj.insertionPoint, midPoint
                   MTextObj.color = acYellow
                   

                    intnum = intnum + NumCout
               Msg = vbCrLf & "Next Internal Point or ENTER to exit: "

          Loop
          On Error GoTo 0

          .SetVariable "OSMODE", 703
          .SetVariable "CMDECHO", 1

     End With

     MsgBox "Done"


End Sub


Matersammichman

  • Guest
Re: create Mtext in the midpoint of a rectangle
« Reply #26 on: June 05, 2007, 08:33:38 AM »
Humbertogo,
Why not create the MText first, then just use BoundingBox to create a rectangle around it?

Dnereb

  • Guest
Re: create Mtext in the midpoint of a rectangle
« Reply #27 on: June 05, 2007, 09:51:40 AM »
the bounding box doesn't detect the text but the grip box of an mtext object.... this can be smaler or biger then the contained text.

Humbertogo

  • Guest
Re: create Mtext in the midpoint of a rectangle
« Reply #28 on: June 05, 2007, 09:59:06 AM »
right

LE

  • Guest
Re: create Mtext in the midpoint of a rectangle
« Reply #29 on: June 05, 2007, 10:00:30 AM »
He wants to place a text in the geometric center or inside of a boundary (Lot), normally will be a rectangle, that's what I understood :).