1
.NET / Re: Linetype scaling differs for unknown reason
« Last post by damien_conroy on May 11, 2024, 04:54:21 PM »the code is pretty sprawling/ugly so I wouldn't know what to include, and I think this might be more of an Autocad skills problem than coding skills problem.
Simplified process for plugin:
Cleanup and prep drawing
calculate size of plot
create layout, control the size by Viewport.ViewHeight (copy paste from Kean Walmsley)
do the plugin functionality, includes drawing some new polyline 2d
here is the section where I create the new polylines, I'm offsetting open polylines so I need to find the direction by measureing a distance to another always present line. The funtionality is pretty much working unless someone has the correct non-idiot way of doing it my problem is is with the linetype scaling.
Simplified process for plugin:
Cleanup and prep drawing
calculate size of plot
create layout, control the size by Viewport.ViewHeight (copy paste from Kean Walmsley)
do the plugin functionality, includes drawing some new polyline 2d
here is the section where I create the new polylines, I'm offsetting open polylines so I need to find the direction by measureing a distance to another always present line. The funtionality is pretty much working unless someone has the correct non-idiot way of doing it my problem is is with the linetype scaling.
Code - C#: [Select]
- using (Transaction trans = db.TransactionManager.StartTransaction())
- {
- try
- {
- BlockTable bt = trans.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
- BlockTableRecord btr = trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
- foreach (ObjectId o in linesToOffset)
- {
- //get the polyline from the blocktable
- Polyline2d ln = trans.GetObject(o, OpenMode.ForWrite) as Polyline2d;
- //test for a bad polyline
- Curve curve = ln as Curve;
- if (curve == null)
- {
- edt.WriteMessage("\nCurve is null");
- logResults["runError"] = "True";
- logResults["comment"] = "Null curve in DoPaintLines()";
- LogMethodCompleted("DoOffset()");
- return false;
- }
- //if the paintline is the correct distance from from the profiles
- //then we find a point to indicate the correct direction to offset.
- //If the offset curve touches the circle with center at a point in
- //the correct direction at the correct distance then we say the offset
- //is on the correct side otherwise reverse the polyline and test again.
- //If test still doesn't work retry a few times
- //Also adjusts polylines style
- bool offset_succeeded = false;
- Int32 offset_iterations = 0;
- while ((!offset_succeeded) && (offset_iterations < offset_max_iterations) && (logResults["runError"] == "False"))
- {
- if (FindTestPt(ln, ref profilePt, ref plinePt, find_test_point_iterations))
- {
- offset_amount = (offset_target * scaleFactor) - offset_begin;
- Vector3d vec = profilePt.GetVectorTo(plinePt).GetNormal().MultiplyBy((offset_target * scaleFactor) + intersectionTolerance * 2);
- Vector3d vec2 = profilePt.GetVectorTo(plinePt).GetNormal().MultiplyBy(offset_target * scaleFactor);
- Point3d offsetTargetPt = profilePt.Add(vec2);
- DBObjectCollection offsetLn = ln.GetOffsetCurves(offset_amount);
- foreach (Polyline2d off in offsetLn)
- {
- off.IntersectWith(testLine, Intersect.OnBothOperands, intersectionPoints, IntPtr.Zero, IntPtr.Zero);
- if (intersectionPoints.Count == 1)
- {
- Double intersectionDistance = intersectionPoints[0].DistanceTo(offsetTargetPt);
- if (intersectionDistance < intersectionTolerance)
- {
- offset_succeeded = true;
- }
- }
- }
- if (!offset_succeeded)
- {
- offsetLn = ln.GetOffsetCurves(offset_amount * -1);
- foreach (Polyline2d off in offsetLn)
- {
- intersectionPoints.Clear();
- off.IntersectWith(testLine, Intersect.OnBothOperands, intersectionPoints, IntPtr.Zero, IntPtr.Zero);
- if (intersectionPoints.Count == 1)
- {
- Double intersectionDistance = intersectionPoints[0].DistanceTo(offsetTargetPt);
- if (intersectionDistance < intersectionTolerance)
- {
- offset_succeeded = true;
- }
- }
- }
- }
- if (offset_succeeded)
- {
- foreach (Polyline2d acEnt in offsetLn)
- {
- btr.AppendEntity(acEnt);
- trans.AddNewlyCreatedDBObject(acEnt, true);
- //acEnt.LinetypeScale = LinetypeScale;
- //acEnt.LineWeight = lineweight;
- acEnt.ConstantWidth = 0.5 * scaleFactor;
- acEnt.Linetype = offsetLineType;
- acEnt.LinetypeScale = offsetLineTypeScale;
- acEnt.LinetypeGenerationOn = true;
- }
- if (!displayDebugHelpers)
- {
- //////////////// DEBUG GRAPHICS //////////////////
- //erase original paintline
- ln.Erase();
- //////////////// DEBUG GRAPHICS END //////////////////
- }
- }
- else
- {
- ln.ConstantWidth = 0.5 * scaleFactor;
- ln.Linetype = offsetLineType;
- ln.LinetypeScale = offsetLineTypeScale;
- ln.LinetypeGenerationOn = true;
- }
- //IntersectCircle(ObjectIdCollection ProfileIDs, Point3d circleCenter, ref Point3d pt)
- if (displayDebugHelpers)
- {
- //////////// DEBUG GRAPHICS //////////////////
- btr.AppendEntity(testLine);
- trans.AddNewlyCreatedDBObject(testLine, true);
- //////////// DEBUG GRAPHICS END //////////////////
- }
- }
- else
- {
- //logResults["runError"] = "True";
- logResults["comment"] = "Failed to find testpoint";
- edt.WriteMessage("\nTestPt not found\n");
- result_paintlines_status = "PaintLineFailed"; //For testing
- }
- offset_iterations++;
- }
- if (!offset_succeeded)
- {
- //logResults["runError"] = "True";
- logResults["comment"] = "Failed after repeated tries";
- }
- }
- //doc.Editor.Regen();
- trans.Commit();
- }
- catch (System.Exception ex)
- {
- //logResults["runError"] = "True";
- logResults["generalException"] = ex.Message;
- edt.WriteMessage("exeption: " + ex.Message + "\n");
- }
- }