How to find the nearest grid point from location of an MText entity

How to find the nearest grid point from location of an MText entity

oneMSN
Advocate Advocate
1,231 Views
6 Replies
Message 1 of 7

How to find the nearest grid point from location of an MText entity

oneMSN
Advocate
Advocate

I want to replace a bunch of MText entities with a block.  The MText entities are off snap, thus there location property is off snap.  I want the replacement block to be inserted on the closest grid snap point to the MText entity being replaced.

I have searched the wild web but cant find anything close to helping with this: Thus I think it must be very easy and I am missing something!

Can anyone point me in the correct direction?

 

 

0 Likes
Accepted solutions (1)
1,232 Views
6 Replies
Replies (6)
Message 2 of 7

SENL1362
Advisor
Advisor

I've used the world WIDE Smiley Happy web and found this:

https://knowledge.autodesk.com/search-result/caas/CloudHelp/cloudhelp/2017/ENU/AutoCAD-NET/files/GUI...

What might help you to determine the nearist grid point.

 

Not clear to me is based on what point of the MText you want to measure, LL, MC, UR...

Can you post a picture showing you're requirements?

 

0 Likes
Message 3 of 7

oneMSN
Advocate
Advocate
Thanks I get how to adjust and set the grid but that's not what i want.

Let's say my grid and snap settings are 3x 3y (no interest in z here).

My MText insertion point might be 3.1234, 3.4321. I wondered if there was a built in method to get the nearest snap position which would be 3,3 .

Now I have a method of rolling my own function to do this i think, but not written it yet or tested it. Before I did this I figured that this must be a common requirement and perhaps the Api had a helper function available to calculate the nearest snap point.

Which is what my question tries to ask.
0 Likes
Message 4 of 7

SENL1362
Advisor
Advisor
Accepted solution

I don't know of any API that can do what you asked for.
But youre problem is more a rounding issue, i.e. pMT=SnapBase+n*(dX, dY)
(assuming the SnapAngle=0)

Message 5 of 7

oneMSN
Advocate
Advocate

Cool, Ill build my own extension method for the Point#d class. 

Thanks

0 Likes
Message 6 of 7

5thSth
Advocate
Advocate

the exact same question was asked a while ago;

https://forums.autodesk.com/t5/net/how-to-automatically-create-a-mtext-spaced-every-100mm/m-p/566767...

 

to quote my self:

 

If I understood you correctly... I dont think it should be a problem.

 

1) prompt for first point3d(2d), prompt for 2nd point3d(2d) 

1***) you may also prompt for distance that you'd like them to be created at... so it's not always 100...

2) spwn new text while their cordinates are < than 2nd point. 

http://through-the-interface.typepad.com/through_the_interface/2010/06/creating-an-autocad-mtext-obj...

 

as for the conditioning... its basic programing concept.

 should look like this;

 

newpoint=startpoint;

while(newpoint+100<endpoint)

{

newpoint=lastpoint+100;

creteMtext(newpoint);

lastpoint=newpoint;

}

 

 P.S. an exception should be made... in case the 2st point coordinate value is "smaller" than the 1st. one... to counter it... a "distance" value should be used to spwn new text.

0 Likes
Message 7 of 7

oneMSN
Advocate
Advocate

I solved this with an Extension method.  Just in case anyone else needs this here's the rough cut code I created.

public static Point2d GetNearestSnapPoint(this Point2d point)
        {
            Point2d snapPoint;

            double snapPointX, snapPointY;

            CalculateSnapCoordinates(point.X, point.Y, out snapPointX, out snapPointY);

            snapPoint = new Point2d(snapPointX, snapPointY);

            return snapPoint;
        }

private static void CalculateSnapCoordinates(
            double originalX, double originalY, out double snapPointX, out double snapPointY)
        {
            var snapIncrements = AutoCADUtilities.GetSnapIncrements();

            double devisorX = originalX / snapIncrements.X;
            double devisorY = originalY / snapIncrements.Y;

            double xMod = Math.Truncate(devisorX);
            double yMod = Math.Truncate(devisorY);

            var addXSnapPoint = Math.Abs(devisorX % 1) > 0.5;
            var addYSnapPoint = Math.Abs(devisorY % 1) > 0.5;


            snapPointX = xMod * snapIncrements.X;
            snapPointY = yMod * snapIncrements.Y;
            if (addXSnapPoint)
                snapPointX += snapIncrements.X;

            if (addYSnapPoint)
                snapPointY += snapIncrements.Y;
        }
0 Likes