.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Attempted to read or write protected memory.

3 REPLIES 3
Reply
Message 1 of 4
rollinson44
950 Views, 3 Replies

Attempted to read or write protected memory.

I have some code (in C#) that I have written that takes a polyline and exports it to a template file that we use for detail sections. The polyline is rotated, and dimension accordingly and then saves the new file based on the name of the associated attribute (selected at the same time as the polyline). It works great; however, I can only run the code once. If I run it another time, without closing the drawing and reopening, I get the following error: "Attempted to read or write protected memory." I'm missing something. I think I disposed of everything, and called the .CloseInput() method for the new database. Can someone please help me with this, it is driving me crazy. I'm running VS 2005 with AutoCad Mechanical 2007. Thanks for your help.
3 REPLIES 3
Message 2 of 4
Anonymous
in reply to: rollinson44

Did you run your code in the debugger to see exactly what line of code the exception is thrown on?

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2009
Supporting AutoCAD 2000 through 2009

http://www.acadxtabs.com

Introducing AcadXTabs 2010:
http://www.caddzone.com/acadxtabs/AcadXTabs2010.htm

wrote in message news:6050214@discussion.autodesk.com...
I have some code (in C#) that I have written that takes a polyline and exports it to a template file that we use for detail sections. The polyline is rotated, and dimension accordingly and then saves the new file based on the name of the associated attribute (selected at the same time as the polyline). It works great; however, I can only run the code once. If I run it another time, without closing the drawing and reopening, I get the following error: "Attempted to read or write protected memory." I'm missing something. I think I disposed of everything, and called the .CloseInput() method for the new database. Can someone please help me with this, it is driving me crazy. I'm running VS 2005 with AutoCad Mechanical 2007. Thanks for your help.
Message 3 of 4
rollinson44
in reply to: rollinson44

Thank for the reply. The error is posted below. Also I uploaded the wrong files earlier, the code that should be here is attached to this post.

Select a polyline: Select an attribute: Attempted to read or write
protected memory. This is often an indication that other memory is corrupt.
acdbmgd
at
?readDwgFile@AcDbDatabase@@$$FQAE?AW4ErrorStatus@Acad@@PB_WH_N0@Z(AcDbDatabase*
, Char* , Int32 , Boolean , Char* )
at Autodesk.AutoCAD.DatabaseServices.Database.ReadDwgFile(String fileName,
FileShare fileSharing, Boolean allowCPConversion, String password)
at BlockExporter.Class1.openTemplate() in ...\BlockExporter\Class1.cs:line 451

Thanks,
Derek

Below is the source code (it is a little lengthy so I apologize, and a little sloppy) the browser wouldn't let me uploaded it:
using System;
using System.Collections.Generic;
using System.Text;
using Autodesk.AutoCAD;
using database = Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.EditorInput;
using acad = Autodesk.AutoCAD;
using app = Autodesk.AutoCAD.ApplicationServices;
using ed = Autodesk.AutoCAD.EditorInput;
using System.Collections;
using System.Windows.Forms;


namespace BlockExporter
{
public static class Class1
{
//static database.TransactionManager templateTrm;
static database.Transaction templateTr;
static database.Database templatedb = new Autodesk.AutoCAD.DatabaseServices.Database(false, true);
static database.BlockTable templateBt;
static database.BlockTableRecord templateBtr;
static acad.ApplicationServices.Document doc;
static acad.Geometry.Point2dCollection p2dColl;
static acad.Geometry.Matrix3d matrix;
static acad.Geometry.Point3dCollection p3dColl;
static ArrayList dims = new ArrayList();

static String[,] pointData;
static double[] pointExtremes = new double[4];
static double rotate = 0;
static String templateLocation;
static String filename = "";
static String strandPattern = "";

[CommandMethod("detail")]
public static void Detail()
{
doc = acad.ApplicationServices.Application.DocumentManager.MdiActiveDocument;

DetailInterface interfaceForm = new DetailInterface();
app.Application.ShowModelessDialog(interfaceForm);

}

public static void dim(PromptEntityResult per, acad.EditorInput.Editor ed, PromptEntityResult attPer)
{
try
{
openTemplate();

acad.DatabaseServices.Database data = doc.Database;

acad.DatabaseServices.Transaction tr = doc.TransactionManager.StartTransaction();

using (tr)
{
database.BlockTable bt = (database.BlockTable)tr.GetObject(data.BlockTableId, Autodesk.AutoCAD.DatabaseServices.OpenMode.ForRead);
database.BlockTableRecord btr = (database.BlockTableRecord)tr.GetObject(bt[database.BlockTableRecord.ModelSpace], Autodesk.AutoCAD.DatabaseServices.OpenMode.ForRead);
acad.DatabaseServices.DBObject obj = tr.GetObject(per.ObjectId, Autodesk.AutoCAD.DatabaseServices.OpenMode.ForRead);
acad.DatabaseServices.Polyline pl = obj as acad.DatabaseServices.Polyline;

p2dColl = new Autodesk.AutoCAD.Geometry.Point2dCollection();
if (pl != null)
{
int vertex = pl.NumberOfVertices;
for (int i = 0; i < vertex; i++)
{
acad.Geometry.Point2d pt = pl.GetPoint2dAt(i);
p2dColl.Add(pt);
//ed.WriteMessage("\n" + pt.ToString());
}

}

double[] blpt = findMiddlePoint(p2dColl);
acad.Geometry.Point3d center3d = new Autodesk.AutoCAD.Geometry.Point3d(408, 594.0620, 0);
matrix = acad.Geometry.Matrix3d.Rotation(getRotate() * Math.PI / 180, acad.Geometry.Vector3d.ZAxis, center3d);

if (pl != null)
{
int vertex = pl.NumberOfVertices;
for (int i = 0; i < vertex; i++)
{
acad.Geometry.Point2d pt = pl.GetPoint2dAt(i);
p2dColl.Add(pt);
//ed.WriteMessage("\n" + pt.ToString());
}

}

acad.Geometry.Point2dCollection tempColl = new Autodesk.AutoCAD.Geometry.Point2dCollection();
for (int i = 0; i <= p2dColl.Count - 1; i++) //cycle through collection and move points to desired location
{
acad.Geometry.Point2d newPt = new Autodesk.AutoCAD.Geometry.Point2d(p2dColl.X - blpt[0] + 408, p2dColl.Y - blpt[1] + 594.0620);
tempColl.Add(newPt);
}
p2dColl = tempColl;

p3dColl = new Autodesk.AutoCAD.Geometry.Point3dCollection(); //collection to hold 3D points
for (int i = 0; i <= p2dColl.Count - 1; i++) //cycle through collection and create 3d points
{
acad.Geometry.Point3d pt3d = new Autodesk.AutoCAD.Geometry.Point3d(p2dColl.X, p2dColl.Y, 0);
p3dColl.Add(pt3d);
}

pointData = new String[p2dColl.Count, 3]; //contains point, orientation, side

acad.Geometry.Point2d tempPt = (acad.Geometry.Point2d)p2dColl[0]; //load initial values into pointExremes for first point in array
pointExtremes[0] = tempPt.X; //left most x initial value
pointExtremes[1] = tempPt.X; // right most x initial value
pointExtremes[2] = tempPt.Y; // upper most y initial value
pointExtremes[3] = tempPt.Y; // lower most y initial value

//compare points to find extreme x and y's
for (int i = 0; i <= p2dColl.Count - 1; i++)
{
acad.Geometry.Point2d comparePt;
acad.Geometry.Point2d pt = (acad.Geometry.Point2d)p2dColl;
if (i == p2dColl.Count - 1)
{
comparePt = (acad.Geometry.Point2d)p2dColl[0];
comparePoints(comparePt, pt, i);
}
else
{
comparePt = (acad.Geometry.Point2d)p2dColl[i + 1];
comparePoints(comparePt, pt, i);
}
checkExtremePoints(pt);
}

//loop to determine if polyline section is horizontal, vertical or skewed and then dimension appropriately using extreme x and y's
for (int i = 0; i <= p2dColl.Count - 1; i++)
{
if (i == p2dColl.Count - 1) //make sure that this is not the last point, if it is then go to else
{
acad.Geometry.Point2d pt1 = (acad.Geometry.Point2d)p2dColl;
acad.Geometry.Point2d pt2 = (acad.Geometry.Point2d)p2dColl[0];
acad.Geometry.Point3d p3t1 = new acad.Geometry.Point3d(pt1.X, pt1.Y, 0);
acad.Geometry.Point3d p3t2 = new acad.Geometry.Point3d(pt2.X, pt2.Y, 0);
acad.Geometry.Point3d p3t3;
double rotation = 0;
if (pointData[i, 1] == "horizontal") //if polyline segment is horizontal find its closest y extreme and dimension in that direction
{
rotation = 0;
if (Math.Abs(pt1.Y - pointExtremes[2]) >= Math.Abs(pt1.Y - pointExtremes[3]))
{
p3t3 = new Autodesk.AutoCAD.Geometry.Point3d(pt1.X, pointExtremes[3] - 60, 0);
}
else
{
p3t3 = new Autodesk.AutoCAD.Geometry.Point3d(pt1.X, pointExtremes[2] + 60, 0);
}
}
else if (pointData[i, 1] == "vertical") //if polyline segment is vertical find closest extreme x and dimension in that direction
{
rotation = 1.570796;
if (Math.Abs(pt1.X - pointExtremes[0]) >= Math.Abs(pt1.X - pointExtremes[1]))
{
p3t3 = new Autodesk.AutoCAD.Geometry.Point3d(pointExtremes[1] + 60, pt1.Y, 0);
}
else
{
p3t3 = new Autodesk.AutoCAD.Geometry.Point3d(pointExtremes[0] - 60, pt1.Y, 0);
}
}
else if (pointData[i, 1] == "skew") //if polyline segment is skewed then dimension to nearest x and y extremes (dimension twice, along x and y axis)
{
rotation = 0;
if (Math.Abs(pt1.Y - pointExtremes[2]) >= Math.Abs(pt1.Y - pointExtremes[3]))
{
p3t3 = new Autodesk.AutoCAD.Geometry.Point3d(pt1.X, pointExtremes[3] - 60, 0);
}
else
{
p3t3 = new Autodesk.AutoCAD.Geometry.Point3d(pt1.X, pointExtremes[2] + 60, 0);
}
acad.DatabaseServices.RotatedDimension dimSkew1 = new acad.DatabaseServices.RotatedDimension(rotation, p3t1, p3t2, p3t3, String.Empty, templatedb.Dimstyle);
dimSkew1.TransformBy(matrix);
dims.Add(dimSkew1);

rotation = 1.570796;
if (Math.Abs(pt1.X - pointExtremes[0]) >= Math.Abs(pt1.X - pointExtremes[1]))
{
p3t3 = new Autodesk.AutoCAD.Geometry.Point3d(pointExtremes[1] + 60, pt1.Y, 0);
}
else
{
p3t3 = new Autodesk.AutoCAD.Geometry.Point3d(pointExtremes[0] - 60, pt1.Y, 0);
}
}
acad.DatabaseServices.RotatedDimension dim = new acad.DatabaseServices.RotatedDimension(rotation, p3t1, p3t2, p3t3, String.Empty, templatedb.Dimstyle);
dim.TransformBy(matrix);
dims.Add(dim);
}
else
{
acad.Geometry.Point2d pt1 = (acad.Geometry.Point2d)p2dColl;
acad.Geometry.Point2d pt2 = (acad.Geometry.Point2d)p2dColl[i + 1];
acad.Geometry.Point3d p3t1 = new acad.Geometry.Point3d(pt1.X, pt1.Y, 0);
acad.Geometry.Point3d p3t2 = new acad.Geometry.Point3d(pt2.X, pt2.Y, 0);
acad.Geometry.Point3d p3t3;
double rotation = 0;

if (pointData[i, 1] == "horizontal") //if horizontal find nearest y extreme and dimension in that direciton
{
rotation = 0;
if (Math.Abs(pt1.Y - pointExtremes[2]) >= Math.Abs(pt1.Y - pointExtremes[3]))
{
p3t3 = new Autodesk.AutoCAD.Geometry.Point3d(pt1.X, pointExtremes[3] - 60, 0);
}
else
{
p3t3 = new Autodesk.AutoCAD.Geometry.Point3d(pt1.X, pointExtremes[2] + 60, 0);
}
}
else if (pointData[i, 1] == "vertical") //if vertical find nearest x extreme and dimension in that direction
{
rotation = 1.570796;
if (Math.Abs(pt1.X - pointExtremes[0]) >= Math.Abs(pt1.X - pointExtremes[1]))
{
p3t3 = new Autodesk.AutoCAD.Geometry.Point3d(pointExtremes[1] + 60, pt1.Y, 0);
}
else
{
p3t3 = new Autodesk.AutoCAD.Geometry.Point3d(pointExtremes[0] - 60, pt1.Y, 0);
}
}
else if (pointData[i, 1] == "skew") //if skew then dimension twice (in x and y planes)
{
rotation = 0;
if (Math.Abs(pt1.Y - pointExtremes[2]) >= Math.Abs(pt1.Y - pointExtremes[3]))
{
p3t3 = new Autodesk.AutoCAD.Geometry.Point3d(pt1.X, pointExtremes[3] - 60, 0);
}
else
{
p3t3 = new Autodesk.AutoCAD.Geometry.Point3d(pt1.X, pointExtremes[2] + 60, 0);
}
acad.DatabaseServices.RotatedDimension dimSkew1 = new acad.DatabaseServices.RotatedDimension(rotation, p3t1, p3t2, p3t3, String.Empty, templatedb.Dimstyle);
dimSkew1.TransformBy(matrix);
dims.Add(dimSkew1);
rotation = 1.570796;
if (Math.Abs(pt1.X - pointExtremes[0]) >= Math.Abs(pt1.X - pointExtremes[1]))
{
p3t3 = new Autodesk.AutoCAD.Geometry.Point3d(pointExtremes[1] + 60, pt1.Y, 0);
}
else
{
p3t3 = new Autodesk.AutoCAD.Geometry.Point3d(pointExtremes[0] - 60, pt1.Y, 0);
}
}
acad.DatabaseServices.RotatedDimension dim2 = new acad.DatabaseServices.RotatedDimension(rotation, p3t1, p3t2, p3t3, String.Empty, templatedb.Dimstyle);
dim2.TransformBy(matrix);
dims.Add(dim2);
}
}

//extract data from selected attribute
acad.DatabaseServices.BlockReference attBRef = (acad.DatabaseServices.BlockReference)tr.GetObject(attPer.ObjectId, Autodesk.AutoCAD.DatabaseServices.OpenMode.ForRead);
acad.DatabaseServices.AttributeCollection attColl = attBRef.AttributeCollection;
acad.DatabaseServices.AttributeReference attRef;

//cycle through each attribute in collection until reaching Tag=Mark# and Tag=Unit then extract that data for text in detail drawing
for (int i = 0; i <= attColl.Count - 1; i++)
{
attRef = (acad.DatabaseServices.AttributeReference)attColl.GetObject(Autodesk.AutoCAD.DatabaseServices.OpenMode.ForRead);
if (attRef.Tag.ToString() == "MARK")
{
filename = attRef.TextString.ToString().Trim();
}
if (attRef.Tag.ToString() == "UNIT")
{
strandPattern = attRef.TextString.ToString().Trim();
}
}
//obj.Dispose();
//pl.Dispose();
//attBRef.Dispose();
//bt.Dispose();
//btr.Dispose();
//data.Dispose();
}
//tr.Dispose();

using (templateTr)
{
//get textstyle for drawing to be added to text in attribute definition
database.TextStyleTable styleTable;
database.TextStyleTableRecord styleTr;
styleTable = (database.TextStyleTable)templatedb.TextStyleTableId.GetObject(Autodesk.AutoCAD.DatabaseServices.OpenMode.ForRead);
styleTr = (database.TextStyleTableRecord)styleTable["STANDARD"].GetObject(Autodesk.AutoCAD.DatabaseServices.OpenMode.ForRead);

//get layer for polyline (our panel)
database.LayerTable laytable;
database.LayerTableRecord laytr;
laytable = (database.LayerTable)templatedb.LayerTableId.GetObject(Autodesk.AutoCAD.DatabaseServices.OpenMode.ForRead);
laytr = (database.LayerTableRecord)laytable["HCPANEL"].GetObject(Autodesk.AutoCAD.DatabaseServices.OpenMode.ForRead);

//create polyline (our panel)
database.Polyline2d newPl = new Autodesk.AutoCAD.DatabaseServices.Polyline2d(Autodesk.AutoCAD.DatabaseServices.Poly2dType.SimplePoly, p3dColl, 0, true, 0, 0, null);
newPl.TransformBy(matrix);
newPl.LayerId = laytr.ObjectId;

//create MText for the Mark#
database.MText fileNameText = new Autodesk.AutoCAD.DatabaseServices.MText();
fileNameText.TextStyle = styleTr.ObjectId;
fileNameText.TextHeight = 15;
fileNameText.Contents = "Mark#: " + filename.ToString();
fileNameText.Location = new Autodesk.AutoCAD.Geometry.Point3d(120.00, 175.00, 0);

//create MText for the standPattern
database.MText strandPtrn = new Autodesk.AutoCAD.DatabaseServices.MText();
strandPtrn.TextStyle = styleTr.ObjectId;
strandPtrn.TextHeight = 15;
strandPtrn.Contents = "Strand Pattern: " + strandPattern.ToString();
strandPtrn.Location = new Autodesk.AutoCAD.Geometry.Point3d(120.00, 155.00, 0);

//add new BlockTableReference and polyline to new drawing
templateBtr.UpgradeOpen();
for (int i = 0; i <= dims.Count - 1; i++)
{
database.RotatedDimension dim = (database.RotatedDimension)dims;
templateBtr.AppendEntity(dim);
templateTr.AddNewlyCreatedDBObject(dim, true);
dim.Dispose();
}
templateBtr.DowngradeOpen();

templateBtr.AppendEntity(newPl);
templateTr.AddNewlyCreatedDBObject(newPl, true);
templateBtr.AppendEntity(fileNameText);
templateTr.AddNewlyCreatedDBObject(fileNameText, true);
templateBtr.AppendEntity(strandPtrn);
templateTr.AddNewlyCreatedDBObject(strandPtrn, true);
templateTr.Commit();

//templateTr.Commit();

//newPl.Dispose();
//strandPtrn.Dispose();
//fileNameText.Dispose();
//styleTable.Dispose();
//styleTr.Dispose();
//laytable.Dispose();
//laytr.Dispose();
//templateBtr.Dispose();
//templateBt.Dispose();

}

//templatedb.CloseInput(true);

templatedb.SaveAs("C:\\" + filename.ToString() + ".dwg", Autodesk.AutoCAD.DatabaseServices.DwgVersion.Current);

templatedb.CloseInput(true);

templatedb.Dispose();

GC.Collect();
//templateTr.Dispose();
}
catch (System.Exception exception)
{
ed.WriteMessage(exception.Message.ToString());
ed.WriteMessage("\n" + exception.Source.ToString());
ed.WriteMessage("\n" + exception.StackTrace.ToString());
}
}

//method determines center of panel to be detailed
private static double[] findMiddlePoint(Autodesk.AutoCAD.Geometry.Point2dCollection tempPoints)
{
double[] middlePoint = new double[4];
middlePoint[0] = tempPoints[0].X;
middlePoint[1] = tempPoints[0].Y;
middlePoint[2] = tempPoints[0].X;
middlePoint[3] = tempPoints[0].Y;

for (int i = 0; i <= tempPoints.Count - 1; i++)
{
if (middlePoint[0] > tempPoints.X)
{
middlePoint[0] = tempPoints.X;
}
if (middlePoint[1] > tempPoints.Y)
{
middlePoint[1] = tempPoints.Y;
}
if (middlePoint[2] < tempPoints.X)
{
middlePoint[2] = tempPoints.X;
}
if (middlePoint[3] < tempPoints.Y)
{
middlePoint[3] = tempPoints.Y;
}
}
double[] middlePt = new double[2];
middlePt[0] = ((middlePoint[2] - middlePoint[0]) / 2 + middlePoint[0]);
middlePt[1] = ((middlePoint[3] - middlePoint[1]) / 2 + middlePoint[1]);
return middlePt; //returns center of panel to be detailed
}

//determines the extreme x and y's for the panel to be detailed
private static void checkExtremePoints(acad.Geometry.Point2d comparePt)
{
if (pointExtremes[0] > comparePt.X )
{
pointExtremes[0] = comparePt.X; //set left extreme x
}
if (pointExtremes[1] < comparePt.X)
{
pointExtremes[1] = comparePt.X; //set right extreme x
}
if (pointExtremes[2] < comparePt.Y)
{
pointExtremes[2] = comparePt.Y; //set upper extreme y
}
if (pointExtremes[3] > comparePt.Y)
{
pointExtremes[3] = comparePt.Y; //set lower extreme y
}
}

//determines if line segments are vertical, horizontal, or skew
private static void comparePoints(acad.Geometry.Point2d comparePt, acad.Geometry.Point2d origPt, int i)
{
if (Math.Abs(comparePt.X - origPt.X) < .01)
{
pointData[i, 0] = origPt.ToString();
pointData[i, 1] = "vertical";
}
else if (Math.Abs(comparePt.Y - origPt.Y) < .01)
{
pointData[i, 0] = origPt.ToString();
pointData[i, 1] = "horizontal";
}
else// if ((Math.Abs(comparePt.X - origPt.X) > .01) && (Math.Abs(comparePt.Y - origPt.Y) > .01))
{
pointData[i, 0] = origPt.ToString();
pointData[i, 1] = "skew";
}
}

//opens the template drawing to be used for the detail
private static void openTemplate()
{
templatedb.ReadDwgFile(getTemplateLocation(), System.IO.FileShare.ReadWrite, true, null);
//templateTrm = templatedb.TransactionManager;
templateTr = templatedb.TransactionManager.StartTransaction();
templateBt = (database.BlockTable)templatedb.BlockTableId.GetObject(Autodesk.AutoCAD.DatabaseServices.OpenMode.ForWrite);
templateBtr = (database.BlockTableRecord)templateBt[database.BlockTableRecord.ModelSpace].GetObject(Autodesk.AutoCAD.DatabaseServices.OpenMode.ForWrite);
}

//sets the template location (set from the form)
public static void setTemplateLocation(String location)
{
templateLocation = location;
}

//returns the template location
private static String getTemplateLocation()
{
return templateLocation;
}

public static void setRotate(Double ro)
{
rotate = ro;
}

public static Double getRotate()
{
return rotate;
}
}
}
Message 4 of 4
Anonymous
in reply to: rollinson44

Sorry, Autodesk has dropped support for NNTP newsreaders, and your code has been reformatted.

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2009
Supporting AutoCAD 2000 through 2009

http://www.acadxtabs.com

Introducing AcadXTabs 2010:
http://www.caddzone.com/acadxtabs/AcadXTabs2010.htm

wrote in message news:6050330@discussion.autodesk.com...
Thank for the reply. The error is posted below. Also I uploaded the wrong files earlier, the code that should be here is attached to this post. Select a polyline: Select an attribute: Attempted to read or write protected memory. This is often an indication that other memory is corrupt. acdbmgd at ?readDwgFile@AcDbDatabase@@$$FQAE?AW4ErrorStatus@Acad@@PB_WH_N0@Z(AcDbDatabase* , Char* , Int32 , Boolean , Char* ) at Autodesk.AutoCAD.DatabaseServices.Database.ReadDwgFile(String fileName, FileShare fileSharing, Boolean allowCPConversion, String password) at BlockExporter.Class1.openTemplate() in ...\BlockExporter\Class1.cs:line 451 Thanks, Derek Below is the source code (it is a little lengthy so I apologize, and a little sloppy) the browser wouldn't let me uploaded it: using System; using System.Collections.Generic; using System.Text; using Autodesk.AutoCAD; using database = Autodesk.AutoCAD.DatabaseServices; using Autodesk.AutoCAD.Runtime; using Autodesk.AutoCAD.EditorInput; using acad = Autodesk.AutoCAD; using app = Autodesk.AutoCAD.ApplicationServices; using ed = Autodesk.AutoCAD.EditorInput; using System.Collections; using System.Windows.Forms; namespace BlockExporter { public static class Class1 { //static database.TransactionManager templateTrm; static database.Transaction templateTr; static database.Database templatedb = new Autodesk.AutoCAD.DatabaseServices.Database(false, true); static database.BlockTable templateBt; static database.BlockTableRecord templateBtr; static acad.ApplicationServices.Document doc; static acad.Geometry.Point2dCollection p2dColl; static acad.Geometry.Matrix3d matrix; static acad.Geometry.Point3dCollection p3dColl; static ArrayList dims = new ArrayList(); static String[,] pointData; static double[] pointExtremes = new double[4]; static double rotate = 0; static String templateLocation; static String filename = ""; static String strandPattern = ""; [CommandMethod("detail")] public static void Detail() { doc = acad.ApplicationServices.Application.DocumentManager.MdiActiveDocument; DetailInterface interfaceForm = new DetailInterface(); app.Application.ShowModelessDialog(interfaceForm); } public static void dim(PromptEntityResult per, acad.EditorInput.Editor ed, PromptEntityResult attPer) { try { openTemplate(); acad.DatabaseServices.Database data = doc.Database; acad.DatabaseServices.Transaction tr = doc.TransactionManager.StartTransaction(); using (tr) { database.BlockTable bt = (database.BlockTable)tr.GetObject(data.BlockTableId, Autodesk.AutoCAD.DatabaseServices.OpenMode.ForRead); database.BlockTableRecord btr = (database.BlockTableRecord)tr.GetObject(bt[database.BlockTableRecord.ModelSpace], Autodesk.AutoCAD.DatabaseServices.OpenMode.ForRead); acad.DatabaseServices.DBObject obj = tr.GetObject(per.ObjectId, Autodesk.AutoCAD.DatabaseServices.OpenMode.ForRead); acad.DatabaseServices.Polyline pl = obj as acad.DatabaseServices.Polyline; p2dColl = new Autodesk.AutoCAD.Geometry.Point2dCollection(); if (pl != null) { int vertex = pl.NumberOfVertices; for (int i = 0; i < vertex; i++) { acad.Geometry.Point2d pt = pl.GetPoint2dAt(i); p2dColl.Add(pt); //ed.WriteMessage("\n" + pt.ToString()); } } double[] blpt = findMiddlePoint(p2dColl); acad.Geometry.Point3d center3d = new Autodesk.AutoCAD.Geometry.Point3d(408, 594.0620, 0); matrix = acad.Geometry.Matrix3d.Rotation(getRotate() * Math.PI / 180, acad.Geometry.Vector3d.ZAxis, center3d); if (pl != null) { int vertex = pl.NumberOfVertices; for (int i = 0; i < vertex; i++) { acad.Geometry.Point2d pt = pl.GetPoint2dAt(i); p2dColl.Add(pt); //ed.WriteMessage("\n" + pt.ToString()); } } acad.Geometry.Point2dCollection tempColl = new Autodesk.AutoCAD.Geometry.Point2dCollection(); for (int i = 0; i <= p2dColl.Count - 1; i++) //cycle through collection and move points to desired location { acad.Geometry.Point2d newPt = new Autodesk.AutoCAD.Geometry.Point2d(p2dColl.X - blpt[0] + 408, p2dColl.Y - blpt[1] + 594.0620); tempColl.Add(newPt); } p2dColl = tempColl; p3dColl = new Autodesk.AutoCAD.Geometry.Point3dCollection(); //collection to hold 3D points for (int i = 0; i <= p2dColl.Count - 1; i++) //cycle through collection and create 3d points { acad.Geometry.Point3d pt3d = new Autodesk.AutoCAD.Geometry.Point3d(p2dColl.X, p2dColl.Y, 0); p3dColl.Add(pt3d); } pointData = new String[p2dColl.Count, 3]; //contains point, orientation, side acad.Geometry.Point2d tempPt = (acad.Geometry.Point2d)p2dColl[0]; //load initial values into pointExremes for first point in array pointExtremes[0] = tempPt.X; //left most x initial value pointExtremes[1] = tempPt.X; // right most x initial value pointExtremes[2] = tempPt.Y; // upper most y initial value pointExtremes[3] = tempPt.Y; // lower most y initial value //compare points to find extreme x and y's for (int i = 0; i <= p2dColl.Count - 1; i++) { acad.Geometry.Point2d comparePt; acad.Geometry.Point2d pt = (acad.Geometry.Point2d)p2dColl; if (i == p2dColl.Count - 1) { comparePt = (acad.Geometry.Point2d)p2dColl[0]; comparePoints(comparePt, pt, i); } else { comparePt = (acad.Geometry.Point2d)p2dColl[i + 1]; comparePoints(comparePt, pt, i); } checkExtremePoints(pt); } //loop to determine if polyline section is horizontal, vertical or skewed and then dimension appropriately using extreme x and y's for (int i = 0; i <= p2dColl.Count - 1; i++) { if (i == p2dColl.Count - 1) //make sure that this is not the last point, if it is then go to else { acad.Geometry.Point2d pt1 = (acad.Geometry.Point2d)p2dColl; acad.Geometry.Point2d pt2 = (acad.Geometry.Point2d)p2dColl[0]; acad.Geometry.Point3d p3t1 = new acad.Geometry.Point3d(pt1.X, pt1.Y, 0); acad.Geometry.Point3d p3t2 = new acad.Geometry.Point3d(pt2.X, pt2.Y, 0); acad.Geometry.Point3d p3t3; double rotation = 0; if (pointData[i, 1] == "horizontal") //if polyline segment is horizontal find its closest y extreme and dimension in that direction { rotation = 0; if (Math.Abs(pt1.Y - pointExtremes[2]) >= Math.Abs(pt1.Y - pointExtremes[3])) { p3t3 = new Autodesk.AutoCAD.Geometry.Point3d(pt1.X, pointExtremes[3] - 60, 0); } else { p3t3 = new Autodesk.AutoCAD.Geometry.Point3d(pt1.X, pointExtremes[2] + 60, 0); } } else if (pointData[i, 1] == "vertical") //if polyline segment is vertical find closest extreme x and dimension in that direction { rotation = 1.570796; if (Math.Abs(pt1.X - pointExtremes[0]) >= Math.Abs(pt1.X - pointExtremes[1])) { p3t3 = new Autodesk.AutoCAD.Geometry.Point3d(pointExtremes[1] + 60, pt1.Y, 0); } else { p3t3 = new Autodesk.AutoCAD.Geometry.Point3d(pointExtremes[0] - 60, pt1.Y, 0); } } else if (pointData[i, 1] == "skew") //if polyline segment is skewed then dimension to nearest x and y extremes (dimension twice, along x and y axis) { rotation = 0; if (Math.Abs(pt1.Y - pointExtremes[2]) >= Math.Abs(pt1.Y - pointExtremes[3])) { p3t3 = new Autodesk.AutoCAD.Geometry.Point3d(pt1.X, pointExtremes[3] - 60, 0); } else { p3t3 = new Autodesk.AutoCAD.Geometry.Point3d(pt1.X, pointExtremes[2] + 60, 0); } acad.DatabaseServices.RotatedDimension dimSkew1 = new acad.DatabaseServices.RotatedDimension(rotation, p3t1, p3t2, p3t3, String.Empty, templatedb.Dimstyle); dimSkew1.TransformBy(matrix); dims.Add(dimSkew1); rotation = 1.570796; if (Math.Abs(pt1.X - pointExtremes[0]) >= Math.Abs(pt1.X - pointExtremes[1])) { p3t3 = new Autodesk.AutoCAD.Geometry.Point3d(pointExtremes[1] + 60, pt1.Y, 0); } else { p3t3 = new Autodesk.AutoCAD.Geometry.Point3d(pointExtremes[0] - 60, pt1.Y, 0); } } acad.DatabaseServices.RotatedDimension dim = new acad.DatabaseServices.RotatedDimension(rotation, p3t1, p3t2, p3t3, String.Empty, templatedb.Dimstyle); dim.TransformBy(matrix); dims.Add(dim); } else { acad.Geometry.Point2d pt1 = (acad.Geometry.Point2d)p2dColl; acad.Geometry.Point2d pt2 = (acad.Geometry.Point2d)p2dColl[i + 1]; acad.Geometry.Point3d p3t1 = new acad.Geometry.Point3d(pt1.X, pt1.Y, 0); acad.Geometry.Point3d p3t2 = new acad.Geometry.Point3d(pt2.X, pt2.Y, 0); acad.Geometry.Point3d p3t3; double rotation = 0; if (pointData[i, 1] == "horizontal") //if horizontal find nearest y extreme and dimension in that direciton { rotation = 0; if (Math.Abs(pt1.Y - pointExtremes[2]) >= Math.Abs(pt1.Y - pointExtremes[3])) { p3t3 = new Autodesk.AutoCAD.Geometry.Point3d(pt1.X, pointExtremes[3] - 60, 0); } else { p3t3 = new Autodesk.AutoCAD.Geometry.Point3d(pt1.X, pointExtremes[2] + 60, 0); } } else if (pointData[i, 1] == "vertical") //if vertical find nearest x extreme and dimension in that direction { rotation = 1.570796; if (Math.Abs(pt1.X - pointExtremes[0]) >= Math.Abs(pt1.X - pointExtremes[1])) { p3t3 = new Autodesk.AutoCAD.Geometry.Point3d(pointExtremes[1] + 60, pt1.Y, 0); } else { p3t3 = new Autodesk.AutoCAD.Geometry.Point3d(pointExtremes[0] - 60, pt1.Y, 0); } } else if (pointData[i, 1] == "skew") //if skew then dimension twice (in x and y planes) { rotation = 0; if (Math.Abs(pt1.Y - pointExtremes[2]) >= Math.Abs(pt1.Y - pointExtremes[3])) { p3t3 = new Autodesk.AutoCAD.Geometry.Point3d(pt1.X, pointExtremes[3] - 60, 0); } else { p3t3 = new Autodesk.AutoCAD.Geometry.Point3d(pt1.X, pointExtremes[2] + 60, 0); } acad.DatabaseServices.RotatedDimension dimSkew1 = new acad.DatabaseServices.RotatedDimension(rotation, p3t1, p3t2, p3t3, String.Empty, templatedb.Dimstyle); dimSkew1.TransformBy(matrix); dims.Add(dimSkew1); rotation = 1.570796; if (Math.Abs(pt1.X - pointExtremes[0]) >= Math.Abs(pt1.X - pointExtremes[1])) { p3t3 = new Autodesk.AutoCAD.Geometry.Point3d(pointExtremes[1] + 60, pt1.Y, 0); } else { p3t3 = new Autodesk.AutoCAD.Geometry.Point3d(pointExtremes[0] - 60, pt1.Y, 0); } } acad.DatabaseServices.RotatedDimension dim2 = new acad.DatabaseServices.RotatedDimension(rotation, p3t1, p3t2, p3t3, String.Empty, templatedb.Dimstyle); dim2.TransformBy(matrix); dims.Add(dim2); } } //extract data from selected attribute acad.DatabaseServices.BlockReference attBRef = (acad.DatabaseServices.BlockReference)tr.GetObject(attPer.ObjectId, Autodesk.AutoCAD.DatabaseServices.OpenMode.ForRead); acad.DatabaseServices.AttributeCollection attColl = attBRef.AttributeCollection; acad.DatabaseServices.AttributeReference attRef; //cycle through each attribute in collection until reaching Tag=Mark# and Tag=Unit then extract that data for text in detail drawing for (int i = 0; i <= attColl.Count - 1; i++) { attRef = (acad.DatabaseServices.AttributeReference)attColl.GetObject(Autodesk.AutoCAD.DatabaseServices.OpenMode.ForRead); if (attRef.Tag.ToString() == "MARK") { filename = attRef.TextString.ToString().Trim(); } if (attRef.Tag.ToString() == "UNIT") { strandPattern = attRef.TextString.ToString().Trim(); } } //obj.Dispose(); //pl.Dispose(); //attBRef.Dispose(); //bt.Dispose(); //btr.Dispose(); //data.Dispose(); } //tr.Dispose(); using (templateTr) { //get textstyle for drawing to be added to text in attribute definition database.TextStyleTable styleTable; database.TextStyleTableRecord styleTr; styleTable = (database.TextStyleTable)templatedb.TextStyleTableId.GetObject(Autodesk.AutoCAD.DatabaseServices.OpenMode.ForRead); styleTr = (database.TextStyleTableRecord)styleTable["STANDARD"].GetObject(Autodesk.AutoCAD.DatabaseServices.OpenMode.ForRead); //get layer for polyline (our panel) database.LayerTable laytable; database.LayerTableRecord laytr; laytable = (database.LayerTable)templatedb.LayerTableId.GetObject(Autodesk.AutoCAD.DatabaseServices.OpenMode.ForRead); laytr = (database.LayerTableRecord)laytable["HCPANEL"].GetObject(Autodesk.AutoCAD.DatabaseServices.OpenMode.ForRead); //create polyline (our panel) database.Polyline2d newPl = new Autodesk.AutoCAD.DatabaseServices.Polyline2d(Autodesk.AutoCAD.DatabaseServices.Poly2dType.SimplePoly, p3dColl, 0, true, 0, 0, null); newPl.TransformBy(matrix); newPl.LayerId = laytr.ObjectId; //create MText for the Mark# database.MText fileNameText = new Autodesk.AutoCAD.DatabaseServices.MText(); fileNameText.TextStyle = styleTr.ObjectId; fileNameText.TextHeight = 15; fileNameText.Contents = "Mark#: " + filename.ToString(); fileNameText.Location = new Autodesk.AutoCAD.Geometry.Point3d(120.00, 175.00, 0); //create MText for the standPattern database.MText strandPtrn = new Autodesk.AutoCAD.DatabaseServices.MText(); strandPtrn.TextStyle = styleTr.ObjectId; strandPtrn.TextHeight = 15; strandPtrn.Contents = "Strand Pattern: " + strandPattern.ToString(); strandPtrn.Location = new Autodesk.AutoCAD.Geometry.Point3d(120.00, 155.00, 0); //add new BlockTableReference and polyline to new drawing templateBtr.UpgradeOpen(); for (int i = 0; i <= dims.Count - 1; i++) { database.RotatedDimension dim = (database.RotatedDimension)dims; templateBtr.AppendEntity(dim); templateTr.AddNewlyCreatedDBObject(dim, true); dim.Dispose(); } templateBtr.DowngradeOpen(); templateBtr.AppendEntity(newPl); templateTr.AddNewlyCreatedDBObject(newPl, true); templateBtr.AppendEntity(fileNameText); templateTr.AddNewlyCreatedDBObject(fileNameText, true); templateBtr.AppendEntity(strandPtrn); templateTr.AddNewlyCreatedDBObject(strandPtrn, true); templateTr.Commit(); //templateTr.Commit(); //newPl.Dispose(); //strandPtrn.Dispose(); //fileNameText.Dispose(); //styleTable.Dispose(); //styleTr.Dispose(); //laytable.Dispose(); //laytr.Dispose(); //templateBtr.Dispose(); //templateBt.Dispose(); } //templatedb.CloseInput(true); templatedb.SaveAs("C:\\" + filename.ToString() + ".dwg", Autodesk.AutoCAD.DatabaseServices.DwgVersion.Current); templatedb.CloseInput(true); templatedb.Dispose(); GC.Collect(); //templateTr.Dispose(); } catch (System.Exception exception) { ed.WriteMessage(exception.Message.ToString()); ed.WriteMessage("\n" + exception.Source.ToString()); ed.WriteMessage("\n" + exception.StackTrace.ToString()); } } //method determines center of panel to be detailed private static double[] findMiddlePoint(Autodesk.AutoCAD.Geometry.Point2dCollection tempPoints) { double[] middlePoint = new double[4]; middlePoint[0] = tempPoints[0].X; middlePoint[1] = tempPoints[0].Y; middlePoint[2] = tempPoints[0].X; middlePoint[3] = tempPoints[0].Y; for (int i = 0; i <= tempPoints.Count - 1; i++) { if (middlePoint[0] > tempPoints.X) { middlePoint[0] = tempPoints.X; } if (middlePoint[1] > tempPoints.Y) { middlePoint[1] = tempPoints.Y; } if (middlePoint[2] < tempPoints.X) { middlePoint[2] = tempPoints.X; } if (middlePoint[3] < tempPoints.Y) { middlePoint[3] = tempPoints.Y; } } double[] middlePt = new double[2]; middlePt[0] = ((middlePoint[2] - middlePoint[0]) / 2 + middlePoint[0]); middlePt[1] = ((middlePoint[3] - middlePoint[1]) / 2 + middlePoint[1]); return middlePt; //returns center of panel to be detailed } //determines the extreme x and y's for the panel to be detailed private static void checkExtremePoints(acad.Geometry.Point2d comparePt) { if (pointExtremes[0] > comparePt.X ) { pointExtremes[0] = comparePt.X; //set left extreme x } if (pointExtremes[1] < comparePt.X) { pointExtremes[1] = comparePt.X; //set right extreme x } if (pointExtremes[2] < comparePt.Y) { pointExtremes[2] = comparePt.Y; //set upper extreme y } if (pointExtremes[3] > comparePt.Y) { pointExtremes[3] = comparePt.Y; //set lower extreme y } } //determines if line segments are vertical, horizontal, or skew private static void comparePoints(acad.Geometry.Point2d comparePt, acad.Geometry.Point2d origPt, int i) { if (Math.Abs(comparePt.X - origPt.X) < .01) { pointData[i, 0] = origPt.ToString(); pointData[i, 1] = "vertical"; } else if (Math.Abs(comparePt.Y - origPt.Y) < .01) { pointData[i, 0] = origPt.ToString(); pointData[i, 1] = "horizontal"; } else// if ((Math.Abs(comparePt.X - origPt.X) > .01) && (Math.Abs(comparePt.Y - origPt.Y) > .01)) { pointData[i, 0] = origPt.ToString(); pointData[i, 1] = "skew"; } } //opens the template drawing to be used for the detail private static void openTemplate() { templatedb.ReadDwgFile(getTemplateLocation(), System.IO.FileShare.ReadWrite, true, null); //templateTrm = templatedb.TransactionManager; templateTr = templatedb.TransactionManager.StartTransaction(); templateBt = (database.BlockTable)templatedb.BlockTableId.GetObject(Autodesk.AutoCAD.DatabaseServices.OpenMode.ForWrite); templateBtr = (database.BlockTableRecord)templateBt[database.BlockTableRecord.ModelSpace].GetObject(Autodesk.AutoCAD.DatabaseServices.OpenMode.ForWrite); } //sets the template location (set from the form) public static void setTemplateLocation(String location) { templateLocation = location; } //returns the template location private static String getTemplateLocation() { return templateLocation; } public static void setRotate(Double ro) { rotate = ro; } public static Double getRotate() { return rotate; } } }

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost