How to determine the drawing units using RealDWG?

How to determine the drawing units using RealDWG?

a.vandegraaf
Participant Participant
2,494 Views
10 Replies
Message 1 of 11

How to determine the drawing units using RealDWG?

a.vandegraaf
Participant
Participant

Hi all, I have a drawing for which I am trying to determine the adopted drawing units in order to apply the correct scale factor when importing the entities in our application which is unit-aware. So far, I have found the following two member functions that provide unit information:

  1. AcDbDatabase::insunits
  2. AcDbBlockTableRecord::blockInsertUnits

However, for the case at hand, both functions return AcDb::kUnitsUndefined (0).

 

Now when I open the drawing in the Autodesk online viewer and measure the length of one of the line segments, it shows the expected value in millimeters, which is in line with my expectation. When I change the unit to something else (say, meters), the numerical value is updated accordingly (i.e. the product of value and unit is invariant). This implies that the viewer is somehow aware of the length unit used (i.e. it is not undefined). Otherwise, if the length unit would be truly unknown, I would expect to see the same numerical value regardless the unit I select.

 

This observation leaves me with the following questions:

  1. I am under the impression that there are more functions that may provide drawing unit information. Is that correct and, if so, what are these functions?
  2. Since there are at least two functions that serve this purpose (at the level of the database as well as the block table record) there is probably some kind of preferred evaluation order. For the two functions that I have found, it makes sense to start with the one at the lowest level (the BTR). If that does not yield any valid unit (i.e. AcDb::kUnitsUndefined) then I would query the database itself. Now if there are more such functions, how do they fit in this ordering? Preferably, I would the drawing unit lookup to follow the same set of rules as in native Autodesk products (assuming such rules exist and are well-defined).

 

Any suggestions or constructive comments are greatly appreciated. For your information: we are using RealDWG 2020.

0 Likes
Accepted solutions (1)
2,495 Views
10 Replies
Replies (10)
Message 2 of 11

tbrammer
Advisor
Advisor

According to the online help AutoCAD uses the varibales INSUNITSDEFSOURCE and INSUNITSDEFTARGET which are stored in the registry if INSUNITS is 0.

It looks like this variable can't be retrieved via AcRxVariablesDictionary, only by acedGetVar().  So I have no idea how to retrieve it via RealDWG. I think your app should use a "locale default unit" i.e. inch for US and mm for Europe and make the unit configurable for the user.


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

0 Likes
Message 3 of 11

a.vandegraaf
Participant
Participant

Hi Thomas, many thanks for your reply. I had come across these system variables that you mentioned when I was searching the web. To me, it was unclear how to retrieve their values, but your answer gives me a hint: acedGetVar(). I think I can call that function from our application code. I will give that a try later today. One more question: when we talk about the units of the current drawing, would that mean that I have to inspect the value of INSUNITSDEFSOURCE or the other one? In other words, is the current drawing thought of as the source or the target?

 

If there is no drawing unit detected, the import mechanism relies on the units currently set in our application. So the user has full control for this particular situation. I just want to make sure that I have checked all relevant information before resorting to this fallback mechanism. Since the Autodesk online viewer gave me the impression that the drawing units could be detected for this particular drawing, I just wanted to know whether I might have missed any relevant information.

0 Likes
Message 4 of 11

moogalm
Autodesk Support
Autodesk Support
Accepted solution

Hi,

 

You should use db.insunits() API, I was wondering you are getting undefined values, I have tested on two attached drawings, I get values as expected.

Can you share the code snippet with us or the drawing that is failing ?, I suspect you might have initialized database properly .

 

acdbHostApplicationServices()->setWorkingDatabase(pDb);

 

 

 

metric-imperial.jpg

0 Likes
Message 5 of 11

a.vandegraaf
Participant
Participant

Hi Madhukar, we already do what you suggest. First, we ensure that a working database has been set ...

acdbHostApplicationServices()->setWorkingDatabase( m_database );

 ... and later on, we call:

auto units = m_database->insunits();

 

I have seen other drawings where this works as expected, but it doesn't for this particular one. Since I received the drawing from one of our users, it is deemed confidential, so I can't share it in this public forum. But it appears that you are affiliated with Autodesk, so if I could send it in some way to you in private, that would be okay. Would that be possible?

 

The file is stored in DXF format, so I am able to inspect its contents with a text editor. These are probably the relevant lines that I have extracted from the file:

$INSUNITS
70
0
9

Does the 0 at the third line stand for AcDb::kUnitsUndefined?

0 Likes
Message 6 of 11

moogalm
Autodesk Support
Autodesk Support

It appears from dxf text, the drawing is unitless.

You should expect '1' if drawing is set imperial, 4 if it is metric [precisely mm]

$INSUNITS
70
1
9

 

Refer: https://www.autodesk.com/techpubs/autocad/acad2000/dxf/header_section_group_codes_dxf_02.htm

 

$INSUNITS

70

Default drawing units for AutoCAD DesignCenter blocks:
0 = Unitless; 1 = Inches; 2 = Feet; 3 = Miles; 4 = Millimeters;
5 = Centimeters; 6 = Meters; 7 = Kilometers; 8 = Microinches;
9 = Mils; 10 = Yards; 11 = Angstroms; 12 = Nanometers;
13 = Microns; 14 = Decimeters; 15 = Decameters;
16 = Hectometers; 17 = Gigameters; 18 = Astronomical units;
19 = Light years; 20 = Parsecs

0 Likes
Message 7 of 11

a.vandegraaf
Participant
Participant

In addition, I have imported your attached drawings in our application and the results are as expected:

  • Imperial.dwg: insunits() returns kUnitsInches (1)
  • metric-drawing.dwg: insunits() returns kUnitsMillimeters (4)

 

So, assuming the drawing doesn't have any unit defined (i.e. kUnitsUndefined), then how does the Autodesk online viewer determine the applicable unit? Is there perhaps some database value which tells whether we are working with Imperial or metric units (or perhaps this is derived from my locale settings)? And, if yes, is there perhaps a default unit assumed once the unit system is known (e.g. "inches" for Imperial, and "millimeters" for metric)?

0 Likes
Message 8 of 11

Alexander.Rivilis
Mentor
Mentor

Try check value of AcDbDatabase::measurement. If value is 1 (metric) - default units is mm, if value is 0 - default units is inch.

Відповідь корисна? Клікніть на "ВПОДОБАЙКУ" цім повідомленням! | Do you find the posts helpful? "LIKE" these posts!
Находите сообщения полезными? Поставьте "НРАВИТСЯ" этим сообщениям!
На ваше запитання відповіли? Натисніть кнопку "ПРИЙНЯТИ РІШЕННЯ" | Have your question been answered successfully? Click "ACCEPT SOLUTION" button.
На ваш вопрос успешно ответили? Нажмите кнопку "УТВЕРДИТЬ РЕШЕНИЕ"


Alexander Rivilis / Александр Ривилис / Олександр Рівіліс
Programmer & Teacher & Helper / Программист - Учитель - Помощник / Програміст - вчитель - помічник
Facebook | Twitter | LinkedIn
Expert Elite Member

Message 9 of 11

a.vandegraaf
Participant
Participant

Hi Alexander, I have tried your suggestion, and this particular function returns kMetric (1) for the drawing at hand. However, I'm not sure how to interpret the meaning of this function. The documentation states:

 

Controls whether the current drawing uses imperial or metric hatch pattern and linetype files.

 

To me it appears to be a setting specifically targeted at hatching and line types, and not so much the general drawing units. But I could be wrong, of course. Perhaps MEASUREMENT has a more general meaning? Could someone affiliated with Autodesk confirm this? In addition, could you please indicate where in the documentation you have found that if measurement() returns kMetric, the unit is "millimeters" and not, say, "meters"? Thank you.

0 Likes
Message 10 of 11

tbrammer
Advisor
Advisor

acedGetVar() is not available in RealDWG. The "ed"  in acedXXX() function names usually implies that they refer to the AutoCAD editor.


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

0 Likes
Message 11 of 11

a.vandegraaf
Participant
Participant

I didn't know that. Thank you for making me aware of this.

0 Likes