I want to create a group hole with an outer circle and an inner circle as the following operation in C# code:
ElementAgenda agenda = new ElementAgenda(); agenda.Insert(innerArcElement, false); agenda.Insert(outerArcElement, false); GroupedHoleElement groupedHole = new GroupedHoleElement(Session.Instance.GetActiveDgnModel(), null, agenda);
but my program crashed at line 4.could someone give me a guide of GroupedHoleElement() func?
Regards
MingQuan
Hi MingQuan,
at first, please follow the best practices and always specify used product and version exactly. To say MSCE is just not enough, because there were nearly 20 CONNECT Edition versions released from the original version to the current CE U15.2.
MingQuan Tang said:but my program crashed at line 4.
It does not tell too much. You are developer, so please provide developer details. What does it mean "crash"? Is there any exception thrown? Is memory dump created? Any such detail can help to find the problem.
MingQuan Tang said:could someone give me a guide of GroupedHoleElement() func?
I recommend to read documentation carefully. GroupedHoleElement constructor is defined as:
public GroupedHoleElement( DgnModel dgnModel, Element solidEeh, ElementAgenda holes )
So I guess it's clear that the second element defines solid part of the grouped hole, whereas agenda collects elements used as holes.
With regards,
Jan
Bentley Accredited Developer: iTwin Platform - AssociateLabyrinth Technology | dev.notes() | cad.point
Answer Verified By: MingQuan Tang
Hello Jan.
I'm newbie in C# addins programing,Sorry for my lack of experience and i can't figure out what's wrong with the crash when new GroupedHoleElement(Session.Instance.GetActiveDgnModel(), outerArcElement, agenda)was called.
May you help me debug this code segment? Really thanks.
public void GroupedHoleTest() { double UorPerMeter = Session.Instance.GetActiveDgnModel().GetModelInfo().UorPerMeter; double UorPerMM = UorPerMeter / 1000; DVector3d yDstVec = DVector3d.FromXYZ(0, 1, 0), zDstVec = DVector3d.FromXYZ(0,0,1); DPoint3d dstPt = DPoint3d.FromXYZ(0, 0, 0); //Draw Inner Circle DVector3d vector0In = new DVector3d(), vector90In = new DVector3d(); double magnitude = 0; yDstVec.TryScaleToLength(1 * UorPerMM, out vector0In, out magnitude); zDstVec.TryScaleToLength(1 * UorPerMM, out vector90In, out magnitude); DEllipse3d dEllipse3D = new DEllipse3d(dstPt, vector0In, vector90In); ArcElement innerArcElement = new ArcElement(Session.Instance.GetActiveDgnModel(), null, dEllipse3D); //innerArcElement.AddToModel(); //Draw Outer Circle yDstVec.TryScaleToLength(1.3 * UorPerMM, out vector0In, out magnitude); zDstVec.TryScaleToLength(1.3 * UorPerMM, out vector90In, out magnitude); dEllipse3D = new DEllipse3d(dstPt, vector0In, vector90In); ArcElement outerArcElement = new ArcElement(Session.Instance.GetActiveDgnModel(), null, dEllipse3D); //outerArcElement.AddToModel(); //Create GroupedHole ElementAgenda agenda = new ElementAgenda(); agenda.Insert(innerArcElement, true); GroupedHoleElement groupedHole = new GroupedHoleElement(Session.Instance.GetActiveDgnModel(), outerArcElement, agenda); return; }
I'm using MSCE Update 15 - Version 10.15.00.74, Could this help?
Hi,
MingQuan Tang said:I'm using MSCE Update 15 - Version 10.15.00.74, Could this help?
probably not now, but the version is the information that has to be shared always, no exception. It's helps to ensure e.g. no old version with specific reported bug is used.
MingQuan Tang said:I'm newbie in C# addins programing
It's also important information. Be aware, to write MicroStation application code, three types of knowledge are required: to know MicroStation (at least as a user, but admin level is better), C# at least on intermediate level and to know MicroStation API.
It's not simple task, so to use available documentation (not only NET, but also C++ doc) and tools (e.g. ILSpy to see how API is implemented, e.g. how arguments are evaluated in constructors and methods) is mandatory. Also, to study SDK examples and topics discussed in this forum is crucial. The most of questions there represent problemd that probably all MicroStation application developer will experience sooner or later.
MingQuan Tang said:May you help me debug this code segment?
Sorry, not. I do not see a chance to invest my time to create new test case in Visual Studio and to debug it, especially when you do not even provide the complete project. Plus, I do not accept the fact you do not provide any single information what really happened, what is reported (e.g. what Exception is thrown) when the code (or the whole MicroStation) crashed. It's your responsibility to debug the code, and, at least, to capture the exception. It's basic dev skill and nothing complicated when Visual Studio is used.
But a few comments, because the code is dirty and it's not clear why the code is written in this way and why it's overcomplicated:
Why this code is used?
double UorPerMeter = Session.Instance.GetActiveDgnModel().GetModelInfo().UorPerMeter; double UorPerMM = UorPerMeter / 1000;
Units (coordinates, length...) is expressed in UORs in MicroStation APIs (VBA is the only exception). When master unis (or sub units) are used in code, it makes sense to have a method to do such conversion (and to follow clean code principles):
private static double MasterToUor(double master) { DgnModel activeModel = Session.Instance.GetActiveDgnModel(); using (ModelInfo modelInfo = activeModel.GetModelInfo()) { double scale = modelInfo.UorPerMaster; return scale * master; } }
The process how vectors are created looks complicated:
... yDstVec.TryScaleToLength(1 * UorPerMM, out vector0In, out magnitude); ...
Isn't is simpler to calculate the value and to create point/vector in the next step?
double yVecPart = MasterToUor(1.23456); // or any similar "to UORs" conversion method DVector3d yDstVec = DVector3d.FromXYZ(0, yVecPart, 0)
Maybe it's simpler to create ellipse in "basic position" and to rotate it in the next step?
ArcElement outerArcElement = new ArcElement(Session.Instance.GetActiveDgnModel(), null, dEllipse3D);
MingQuan Tang said:i can't figure out what's wrong with the crash
What exception is reported? Without this information, not further analysis can be done.
MingQuan Tang said:this code segment?
I see the code as often beginners problem: You want to do everything at once. Not only that the code is dirty (not following KISS principle), but also it's hard to guess (even when debugged step by step) where is the source of the problem (and not where the problem is presented in a form of crash or exception.
When you are beginner (I am not sure whether you mean you are C# dev beginner or you know C# but you have no experience with MicroStation API), I recommend to learn and test the code step by step:
Bad BentleyStatus:32768
************** Exception Text **************Bentley.DgnPlatformNET.DgnPlatformNETException: Bad BentleyStatus: 32768 at Bentley.DgnPlatformNET.StatusHandler.ThrowStatusException(String statusType, Int32 status) at Bentley.DgnPlatformNET.Elements.GroupedHoleElement..ctor(DgnModel dgnModel, Element solidEeh, ElementAgenda holes) at csAddins.HuaDongZhiJia.CreateElement(DVector3d yDstVec, DVector3d zDstVec, DPoint3d dstPt, Double scale) in D:\Visual Studio Projects\Projects\csAddins\csAddins\Hanger.cs:line 81
the line 81 is where new GroupedHoleElement() was called.
Jan Šlegr said:Use elements existing in DGN file, try to create grouped hole element. Does it work?
It works ok in Microstation
Jan Šlegr said:Create the same elements in the code, place them to model (so it can be checked they exist and they are correct), try to create grouped hole element. Does it work?
Place to model is fine but cant create grouped hole element
Jan Šlegr said:Do not add the elements to model, create grouped hole element directly. Does it work?
This is same as last one.
It throws exception in Microstation's Exception Text but i cant get it in Visual Studio debug mode.
I use try catch and get this exception as follow :
try { GroupedHoleElement groupedHole = new GroupedHoleElement(Session.Instance.GetActiveDgnModel(), outerArcElement, agenda); } catch(Exception e) { string exceptionText = e.Message; }
MingQuan Tang said:It throws exception in Microstation's Exception Text but i cant get it in Visual Studio debug mode
You must attach your Viz Studio project to the MicroStation process.
MingQuan Tang said:The exception message is "Bad BentleyStatus:32768"
Bentley status 32768 is a general-purpose value meaning 'Something went wrong'. It's a short unsigned integer set to 0xFFFF.
Regards, Jon Summers LA Solutions
Thanks Jon.
My program crashed because GroupedHoleElement() seems can't pass ArcElements as parameter.
When using EllipseElement it works fine.