Create a new layer (C#)

Create a new layer (C#)

Anonymous
Not applicable
1,377 Views
8 Replies
Message 1 of 9

Create a new layer (C#)

Anonymous
Not applicable
Ok...I thought I would make a new thread and see what you guys thought.....

I would like to add the ability to make the layer a plot layer or a no plot layer but am not sure whether that is a boolean for PlotStyle property or what?

I am taking all the parameters from a Access database so we can just assume the parameters below are correct.

Any other suggestions or thoughts?





{code}
public void createLayer(string layerName, int layerColor, string layerLineType, string layerDescription)
{
Document doc = Application.DocumentManager.MdiActiveDocument;

Database db = doc.Database;
//start transaction
using (Transaction tr = db.TransactionManager.StartTransaction())
{

try
{

//Get current layerTable
LayerTable lt = (LayerTable)tr.GetObject(db.LayerTableId, OpenMode.ForWrite);

//Make new layTable record
LayerTableRecord ltr = new LayerTableRecord();

//Set Layer Properties
ltr.Name = layerName;

//LayerColor value is being sent in as a int32 to converting to int32 fo byACi colorMethod. I am using numbers such as 251 and 5 as colors.
Int16 layerColorShort = Convert.ToInt16(layerColor);
ltr.Color = Autodesk.AutoCAD.Colors.Color.FromColorIndex(Autodesk.AutoCAD.Colors.ColorMethod.ByAci, layerColorShort);
ltr.Description = layerDescription;

// get linetypeId....do line types always have the same id...for example is Continuous always the same id?
LinetypeTable lt2 = (LinetypeTable)tr.GetObject(db.LinetypeTableId, OpenMode.ForRead);
foreach (ObjectId lineid in lt2)
{
LinetypeTableRecord ltr2 = (LinetypeTableRecord)tr.GetObject(lineid, OpenMode.ForRead);
if (ltr2.Name == layerLineType)
{

ltr.LinetypeObjectId = ltr2.Id;
}
}
//add newly created layer back to layerTable
lt.Add(ltr);
tr.AddNewlyCreatedDBObject(ltr, true);
tr.Commit();

}

catch
{

tr.Abort();

}
}
{code}
0 Likes
1,378 Views
8 Replies
Replies (8)
Message 2 of 9

Anonymous
Not applicable
by the way....I read a sticky at the main page of the forum and learned that to keep it in code format you use:

\{code}
code here
\{code} Edited by: joelkarr on Dec 1, 2008 11:32 PM
0 Likes
Message 3 of 9

Anonymous
Not applicable
Hi joel,

Which clearly didn't work on your first post.

Try


Put your code here.
This has always worked for me


Regards


Laurie Comerford

joelkarr wrote:
> by the way....I read a sticky at the main page of the forum and learned
> that to keep it in code format you use: \{code} code here \{code} Edited
> by: joelkarr on Dec 1, 2008 11:32 PM
0 Likes
Message 4 of 9

Anonymous
Not applicable

Laurie doesn't understand that the \{code} tags preserve the
formatting of code when viewed via the web interface.

 

Unfortuantely, for those of us who want no part of the
stinking, dysfunctional pile of garbage web interface, code formatting is not
preserved in newsreaders.

 

If you want the benefit of more of your peers, my advice
would be to switch to a newsreader for accessing the newsgroups.


 

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

href="http://www.acadxtabs.com">http://www.acadxtabs.com

 


 

 


style="PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">
by
the way....I read a sticky at the main page of the forum and learned that to
keep it in code format you use: \{code} code here \{code} Edited by: joelkarr
on Dec 1, 2008 11:32 PM
0 Likes
Message 5 of 9

chiefbraincloud
Collaborator
Collaborator
You use LayerTableRecord.IsPlottable - Simple Read/Write Boolean property.
Dave O.                                                                  Sig-Logos32.png
0 Likes
Message 6 of 9

Anonymous
Not applicable
thanks chief. I plan on having more code to come so hopefully it can at least serve as a starting point for new .net autocad developers.

And its always nice to have someone else check my code and make suggestions haha.

Next on the list is removing a set of layers but prompting the user to move any objects to the 0 layer if they exist on a layer to be deleted.
0 Likes
Message 7 of 9

Anonymous
Not applicable
Do you want to prompt them, or just automatically move the items yourself? I can see value in both. If you prompt them, then we can assume that anything left over is to be marked for deletion.



Either way your going to have to cycle through all entities in the block table looking for anything that happens to have that layer as its property in order to change/erase the enitity before you remove the layer. (even though the layer will still exist if you only mark it for erase)



Would be nice if a layer can tell you what entities are using it... Any ideas out there?



jvj
0 Likes
Message 8 of 9

Anonymous
Not applicable
I was thinking to prompt them to either A. Do not remove layers with objects on them or B. move objects to 0 layer and remove layers

Here's a start from Kean Wamsley at http://through-the-interface.typepad.com

{code}
private static ObjectIdCollection

GetEntitiesOnLayer(string layerName)

{

Document doc =

Application.DocumentManager.MdiActiveDocument;

Editor ed = doc.Editor;

// Build a filter list so that only entities

// on the specified layer are selected

TypedValue[] tvs =

new TypedValue[1] {

new TypedValue(

(int)DxfCode.LayerName,

layerName

)

};

SelectionFilter sf =

new SelectionFilter(tvs);

PromptSelectionResult psr =

ed.SelectAll(sf);

if (psr.Status == PromptStatus.OK)

return

new ObjectIdCollection(

psr.Value.GetObjectIds()

);

else

return new ObjectIdCollection();

}
{code} Edited by: joelkarr on Dec 2, 2008 3:49 PM
0 Likes
Message 9 of 9

Anonymous
Not applicable
Hi Tony,

The fact that "\{code}" tags work on the Web interface is irrelevant.
They don't work anywhere else and that is sufficient reason to criticise
them. It's not OK to use them and expect all users who want to help are
going to be able to see posted code properly formatted.

The whole newsgroup situation is absolutely pathetic and the newsgroup
manager should be sacked for absolute incompetence.

Regards


Laurie Comerford

Tony Tanzillo wrote:
> Laurie doesn't understand that the \{code} tags preserve the formatting
> of code when viewed via the web interface.
>
> Unfortuantely, for those of us who want no part of the stinking,
> dysfunctional pile of garbage web interface, code formatting is not
> preserved in newsreaders.
>
> If you want the benefit of more of your peers, my advice would be to
> switch to a newsreader for accessing the newsgroups.
>
> --
>
> 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:6081982@discussion.autodesk.com...
> by the way....I read a sticky at the main page of the forum and
> learned that to keep it in code format you use: \{code} code here
> \{code} Edited by: joelkarr on Dec 1, 2008 11:32 PM
>
0 Likes