PDA

View Full Version : Routine to create a new Layer and set Color



BunyipCatcher
2006-04-06, 02:32 PM
Morning All,

I have tried writing the below function to create a new layer. But it type
mismatches on the .TrueColor = intColourNumber line. How can I set the
colour of my layer. Also is it necessary to set variables to nothing at the
end or they will disappear automatically when the function is finished?


Public Function CreateNewLayer(ByVal strLayerName As String, intColourNumber
As Integer) As AcadLayer
Dim objAcadLayer As AcadLayer

'Create and reference the new layer
Set objAcadLayer = ThisDrawing.Layers.Add(strLayerName)

'Set layer properties
With objAcadLayer
.TrueColor = intColourNumber
End With

Set CreateNewLayer = objAcadLayer

Set objAcadLayer = Nothing
End Function

burling
2006-04-06, 09:15 PM
Hi,

Try using "color" instead of TrueColor



Public Function CreateNewLayer(ByVal strLayerName As String, intColourNumber
As Integer) As AcadLayer
Dim objAcadLayer As AcadLayer

'Create and reference the new layer
Set objAcadLayer = ThisDrawing.Layers.Add(strLayerName)

'Set layer properties
With objAcadLayer
.Color = intColourNumber
End With

Set CreateNewLayer = objAcadLayer

Set objAcadLayer = Nothing
End Function


as far as i know there is no need to reset your variables at the end of the code

Regards,

Brett

rkmcswain
2006-04-07, 12:19 AM
Try using "color" instead of TrueColor


Depends on the version. "Color" is an obsolete property.

TrueColor should be used.

Look about 3/4 way down this post -> http://groups.google.com/group/autodesk.autocad.customization.vba/msg/46d6e1ce39d929f7?hl=en&

burling
2006-04-09, 08:49 PM
Depends on the version. "Color" is an obsolete property.


In what version has "Color" become Obsolete?

rkmcswain
2006-04-10, 12:18 AM
In what version has "Color" become Obsolete?

Not quite sure, but I would guess 2004, when TrueColor was introduced. I'm looking at the 2006 version of "acad_dev.chm"

According to this post, it was 2004.
http://groups.google.com/group/autodesk.autocad.customization.vba/msg/ad81e0ed91e585dd?hl=en&

burling
2006-04-10, 03:48 AM
Strange, i'm using AutoCAD 2006 and for all my coding i use the color property
for example:


objline.color=1

or

objline.color=acWhite



Seems to work everytime....

fixo
2006-04-10, 08:17 AM
Morning All,

I have tried writing the below function to create a new layer. But it type
mismatches on the .TrueColor = intColourNumber line. How can I set the
colour of my layer. Also is it necessary to set variables to nothing at the
end or they will disappear automatically when the function is finished?



Try this one, seems to work for me (A2005):


Option Explicit
Public Sub Create_Layer()
Dim lName As String
Dim layObj As AcadLayer
Dim acmCol As AcadAcCmColor
Dim ltName As String
Dim ltpObj As AcadLineType
Dim colNdx As String
Dim lrWgt As String
Dim pltFlag As Long

lName = InputBox("Enter layer name: ")
If "" = lName Then Exit Sub
On Error Resume Next

Set layObj = ThisDrawing.Layers(lName)
If layObj Is Nothing Then
Set layObj = ThisDrawing.Layers.Add(lName)
End If

colNdx = InputBox("Enter layer color number: " & _
vbNewLine & " [0 to 255]" & _
vbNewLine & " default color red")
If "" = colNdx Then
colNdx = "10"
End If

Set acmCol = AcadApplication.GetInterfaceObject("AutoCAD.AcCmColor.16")
With acmCol
.ColorMethod = acColorMethodByACI
.ColorIndex = colNdx
End With
layObj.TrueColor = acmCol


ltName = InputBox("Enter a Linetype name" & vbNewLine _
& " to set for layer " & vbNewLine & "[case-sensitive] :")
If "" = ltName Then
ltName = "Continuous"
End If

Set ltpObj = ThisDrawing.Linetypes(ltName)
If ltpObj Is Nothing Then
ThisDrawing.Linetypes.Load ltName, "acad.lin"
End If
If Err Then
layObj.Linetype = "Continuous"
Else
layObj.Linetype = ltName
End If

lrWgt = InputBox("Enter a lineweight value" & " [from 0 to 211] " & _
vbNewLine & "[0/5/9/13/15/18/20/25/30/35/40/45/50/53/60/ etc.]:")
If "" = lrWgt Then
lrWgt = acLnWtByLwDefault
Else
lrWgt = CLng(lrWgt)
End If
layObj.Lineweight = lrWgt
If Err Then
layObj.Lineweight = acLnWtByLwDefault
End If

pltFlag = MsgBox("Need to set layer to 'noplot'?", vbYesNo)
If pltFlag = vbYes Then
layObj.Plottable = False
Else
layObj.Plottable = True
End If

MsgBox Err.Description

End Sub


f.

rkmcswain
2006-04-10, 11:29 AM
Strange, i'm using AutoCAD 2006 and for all my coding i use the color property
Seems to work everytime....

Left in so it wouldn't break existing code.

The following is taken right out of the 2007 version of acad_dev.chm.

This property is obsolete and will be removed in a future version.

I suppose they may leave it in for a while, but you never know.

MikeJarosz
2006-04-10, 03:32 PM
Simple question regarding the thread above: Does ACAD accept the British spelling of color (colour), or might this be part of the problem?

I know when I worked in London, their software went crazy when I used US spellings.