COM AddRegion
The following Delphi code ends up in an exception "wrong parameter". Same code works in AutoCAD.
var
A: OleVariant;
Doc: IAcadDocument;
Poly: IAcadLWPolyline;
RegObjs: OleVariant;
begin
A := VarArrayCreate([0, 0], varDispatch);
Poly := Doc.ModelSpace.AddLightWeightPolyline(Points);
Poly.Closed := True;
A[0] := Poly;
RegObjs := ADoc.ModelSpace.AddRegion(A); <-- exception here
end;
Comments
-
Hi Alex,
A couple of observations:
1. Is your Doc variable being intialised correctly? You have a local variable "Doc" but you don't show where it is instantiated so I'm surprised you're not getting an access violation on the "AddLightwieghtPolyline" line.
2. You change to a global variable "ADoc". Is this variable being initialised?
3. The description for AddRegion says that the passed in objects must all be coplanar. Is this the case?
Lastly, can you supply the entirety of the function that works in Autocad as it is difficult to test without the data you're testing it with.
Regards,
Daniel
0 -
This seems more difficult than I thougt! Yes the ADoc is initialized. The variables are more informational.
I've investigated so far that it has to do with the points and they are coplanar.
I draw a line to intersect the polyline with the command IntersectWith(Line, acExtendOtherEntity). The Points for the line are given as Double. This results in two intersection points which I take to draw a new polyline and this is taken to create the region. If I define the points for the line as more precise type Currency, the final polyline is ok for the region otherwise not.The problem seems to be the internal handling of coordinates as OleVariants in a point array which is in Delphi created as VarArrayCreate([0, x], varDouble). I have no clue why the type Double passed to a point array does not work.
0 -
It may be a rounding issue but I can't reproduce it; the below test code works fine and it is almost identical to yours. The Currency format is not more precise than Double; in fact it only preserves four decimal places of precision. I had noticed a couple of variant handling issues at one time but I found a workaround and never thought to revisit the problem. But again, without your actual code to test with there isn't much more help I can provide. The only way I was able to create the error you received was to pass the LWPolyline function a 3D list of points instead of 2D but this didn't cause the problem at the point you showed but at the creation of the Lightweight Polyline?
Regards,
procedure TTestForm.Button1Click(Sender: TObject);Var A : OleVariant; App : IAcadApplication; Doc : IAcadDocument; Poly : IAcadLWPolyline; RegObjs : OleVariant; Points : OleVariant;begin App := IDispatch(GetActiveOleObject('BricscadApp.AcadApplication')) as IAcadApplication; Doc := App.ActiveDocument; A := VarArrayCreate([0,0], varDispatch); Points := VarArrayCreate([0,5], varDouble); Points[0] := 0.0; Points[1] := 0.0; Points[2] := 10.0; Points[3] := 10.0; Points[4] := 10.0; Points[5] := 0.0; Poly := Doc.ModelSpace.AddLightWeightPolyline(Points); Poly.Closed := True; A[0] := Poly; RegObjs := Doc.ModelSpace.AddRegion(A); //<-- exception hereend;
0 -
Daniel, thank you for your effort. Of course you're right about the data type currency;-)
Meanwhile I have installed the latest Version and after restarting the computer, the problem has gone.
0 -
Excellent glad to hear it. Nice to see other Delphi developers extending *CAD too!
0