How to edit part of the dtext?

How to edit part of the dtext?

swaywood
Collaborator Collaborator
994 Views
9 Replies
Message 1 of 10

How to edit part of the dtext?

swaywood
Collaborator
Collaborator

Hi:

I want to edit a part of the dtext, the step like:

1.run the command

2.select two points,get '89a'

S-628.png

3.input the replace character '5'

4.get the result dtext

S-629.png

0 Likes
Accepted solutions (1)
995 Views
9 Replies
Replies (9)
Message 2 of 10

tbrammer
Advisor
Advisor

First you need the DBText object itself. If you have the two points p1 and p2 of the enclosing rectangle you can use them to retrieve a selection set that contains the DBText. Next have to find the indices of the first and last character that is within the rectangle.

I would suggest to create a DBText object with the same properties as the selected one, and evaluate its extents with different TextString lengths:

 

 

Point3d p1, p2; // The selected Points. 
// p1 must be "minpoint" and p2 "maxpoint": (p1[k] < p2[k]) must hold for all k
DBText txtOrig = ...  // Your text object
DBText txt = txtOrig; // temporary copy of the text object
Extents3d extPrev = new Extents3d();
int iFirst = -1, iLast = -1; // Index of first and last character within [p1 p2]
for (int n=1; n<=txtOrig.TextString.Length; ++n)
{
	// Analyze the extents of a DBText with the first n characters of the original text
	txt.TextString = txtOrig.TextString.Substring(0, n);
	Extents3d ext = txt.GeometricExtents;
	extPrev = ext;

	// Get the extents of the n-th character
	Extents3d ext_n = new Extents3d();
	if (n == 0)
		ext_n = ext;
	else
	{
		Point3d min = new Point3d(extPrev.MaxPoint.X, ext.MinPoint.Y, ext.MinPoint.Z);
		ext_n.Set(min, ext.MaxPoint);
	}

	// Does ext_n overlap with the selected rectangle?
	if (ext_n.MaxPoint.X > p1.X && ext_n.MinPoint.Y < p2.X ) // within X-extents?
	{
		// n-th char is within the [p1 p2] rectangle
		if (iFirst < 0)
			iFirst = n;
		iLast = n;
	}
// Now you can replace chars iFirst...iLast }

 

Note that the code above doesn't consider multiline text and text rotation.


Thomas Brammer ● Software Developer ● imos AGLinkedIn
If an answer solves your problem please [ACCEPT SOLUTION]. Otherwise explain why not.

0 Likes
Message 3 of 10

swaywood
Collaborator
Collaborator

thank you. I tried the code, but the %%c…is a big problem.

0 Likes
Message 4 of 10

ActivistInvestor
Mentor
Mentor

 

If the intent with the highlighted line below was to create a temporary copy of the DBText, you need to call Clone(), otherwise it's operating on the 'textOrig', which you probably don't want to do.

 

 
@tbrammer wrote:

First you need the DBText object itself. If you have the two points p1 and p2 of the enclosing rectangle you can use them to retrieve a selection set that contains the DBText. Next have to find the indices of the first and last character that is within the rectangle.

I would suggest to create a DBText object with the same properties as the selected one, and evaluate its extents with different TextString lengths:

 

 

Point3d p1, p2; // The selected Points. 
// p1 must be "minpoint" and p2 "maxpoint": (p1[k] < p2[k]) must hold for all k
DBText txtOrig = ...  // Your text object
DBText txt = txtOrig; // temporary copy of the text object
Extents3d extPrev = new Extents3d();
int iFirst = -1, iLast = -1; // Index of first and last character within [p1 p2]
for (int n=1; n<=txtOrig.TextString.Length; ++n)
{
	// Analyze the extents of a DBText with the first n characters of the original text
	txt.TextString = txtOrig.TextString.Substring(0, n);
	Extents3d ext = txt.GeometricExtents;
	extPrev = ext;

	// Get the extents of the n-th character
	Extents3d ext_n = new Extents3d();
	if (n == 0)
		ext_n = ext;
	else
	{
		Point3d min = new Point3d(extPrev.MaxPoint.X, ext.MinPoint.Y, ext.MinPoint.Z);
		ext_n.Set(min, ext.MaxPoint);
	}

	// Does ext_n overlap with the selected rectangle?
	if (ext_n.MaxPoint.X > p1.X && ext_n.MinPoint.Y < p2.X ) // within X-extents?
	{
		// n-th char is within the [p1 p2] rectangle
		if (iFirst < 0)
			iFirst = n;
		iLast = n;
	}
// Now you can replace chars iFirst...iLast }

 

Note that the code above doesn't consider multiline text and text rotation.


 

0 Likes
Message 5 of 10

swaywood
Collaborator
Collaborator

thanks ActivistInvestor

I know the code has some little problem.

The difficult is not this, but the '%%c,\uxxxxx' charactors, i don't know how to solve it.

 

could any body give me some code to do this?

 

thanks 

0 Likes
Message 6 of 10

ActivistInvestor
Mentor
Mentor
Accepted solution

If the string contains %%c or escaped unicode, you can use the Unicode.Encoding class to deal with that, but I suppose you're going to have to deal with the AutoCAD-specific %% codes by parsing the string into tokens and treating each sequence as a single character.

 


@swaywood wrote:

thanks ActivistInvestor

I know the code has some little problem.

The difficult is not this, but the '%%c,\uxxxxx' charactors, i don't know how to solve it.

 

could any body give me some code to do this?

 

thanks 


 

Message 7 of 10

swaywood
Collaborator
Collaborator

parse the %%c int tokens, but what is the token, I found different charactor has different width.

when i change the %%c into Φ the width changed too.

Work-220.pngWork-219.pngWork-218.pngWork-217.png

 

0 Likes
Message 8 of 10

tbrammer
Advisor
Advisor

See here for a list of special characters.

Your screenshot shows two different characters: The diameter sign ∅ has the Unicode charcode U+2205 and Φ (greek Phi) U+03A6.


Thomas Brammer ● Software Developer ● imos AGLinkedIn
If an answer solves your problem please [ACCEPT SOLUTION]. Otherwise explain why not.

0 Likes
Message 9 of 10

swaywood
Collaborator
Collaborator

anyone done this before?

0 Likes
Message 10 of 10

swaywood
Collaborator
Collaborator

Hi @ActivistInvestor,  parsing the string into tokens is better way.

0 Likes