03-05 创建和编辑AutoCAD实体(五) 使用图层、颜色和线型(2)使用颜色

前端之家收集整理的这篇文章主要介绍了03-05 创建和编辑AutoCAD实体(五) 使用图层、颜色和线型(2)使用颜色前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

2、Work with Colors使用颜色

You can assign a color to an individual object in a drawing using its Color or ColorIndex property. The ColorIndex property accepts an AutoCAD Color Index (ACI) value as a numeric value of 0 - 256. The Color property is used to assign an ACI number,true color,or color book color to an object. To change the value of the Color property,you use the Color object which is under the Colors namespace.

我们可以使用Color属性或ColorIndex属性给图形中的每个对象设置颜色。ColorIndex属性接受0-256的AutoCAD颜色索引值。Color属性则可以使用ACI值、真彩色值、或配色系统值来给对象设置颜色。我们使用Colors命名空间的Color对象来修改Color属性的值。

The Color object has a SetRGB method which allows you to choose from millions of color combinations based on mixing a red,green and blue color value together. The Color object also contains methods and properties for specifying color names,color books,color indexes,and color values.

Color对象提供了一个SetRGB方法,允许我们从数百万个红绿蓝颜色组合中选择颜色。Color对象还包含指定颜色名称、配色系统、颜色索引、及颜色值的方法属性

You can also assign colors to layers. If you want an object to inherit the color of the layer it is on,set the object’s color to ByLayer by setting its ACI value to 256. Any number of objects and layers can have the same color number. You can assign each color number to a different pen on a pen plotter or use the color numbers to identify certain objects in the drawing,even though you cannot see the colors on your screen.

我们还可以给图层设置颜色。如果要让对象继承所在图层的颜色,只需将对象的颜色设置为ByLayer即可(通过将ACI值设置为256实现)。不同的对象和图层可以拥有相同的颜色值。我们可以给彩笔绘图仪的不同的彩笔赋予一个颜色值,或者在图形中使用颜色值表示特定的对象,即便我们在屏幕上看不到所设的颜色。

For more information about working with colors,see “Work with Colors” in the AutoCAD User's Guide.

关于使用颜色的更多内容,见AutoCAD用户指南的“使用颜色”。

Topics in this section

· Assign a color value to an object 给对象颜色赋值

· Make a color current through the database 通过数据库设置当前颜色

· Make a color current with the CECOLOR system variable 使用系统变量CECOLOR设置当前颜色

2.1、Assign a color value to an object给对象颜色赋值

The following example creates 4 circles and assigns a different color to each circle using four different methods.

下面例子创建4个圆,并使用四种不同方法为每个圆指定不同的颜色。

VB.NET

Imports Autodesk.AutoCAD.Runtime

Imports Autodesk.AutoCAD.ApplicationServices

Imports Autodesk.AutoCAD.DatabaseServices

Imports Autodesk.AutoCAD.Geometry

Imports Autodesk.AutoCAD.Colors

<CommandMethod("SetObjectColor")> _

Public Sub SetObjectColor()

'' Get the current document and database

Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument

Dim acCurDb As Database = acDoc.Database

'' Start a transaction

Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()

'' Define an array of colors for the layers

Dim acColors(2) As Color

acColors(0) = Color.FromColorIndex(ColorMethod.ByAci,1)

acColors(1) = Color.FromRgb(23,54,232)

acColors(2) = Color.FromNames("PANTONE Yellow 0131 C",_

"PANTONE(R) pastel coated")

'' Open the Block table for read

Dim acBlkTbl As BlockTable

acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,_

OpenMode.ForRead)

'' Open the Block table record Model space for write

Dim acBlkTblRec As BlockTableRecord

acBlkTblRec = acTrans.GetObject(acBlkTbl(BlockTableRecord.ModelSpace),_

OpenMode.ForWrite)

'' Create a circle object and assign it the ACI value of 4

Dim acPt As Point3d = New Point3d(0,3,0)

Dim acCirc As Circle = New Circle()

acCirc.Center = acPt

acCirc.Radius = 1

acCirc.ColorIndex = 4

acBlkTblRec.AppendEntity(acCirc)

acTrans.AddNewlyCreatedDBObject(acCirc,True)

Dim nCnt As Integer = 0

While (nCnt < 3)

'' Create a copy of the circle

Dim acCircCopy As Circle

acCircCopy = acCirc.Clone()

'' Shift the copy along the Y-axis

acPt = New Point3d(acPt.X,acPt.Y + 3,acPt.Z)

acCircCopy.Center = acPt

'' Assign the new color to the circle

acCircCopy.Color = acColors(nCnt)

acBlkTblRec.AppendEntity(acCircCopy)

acTrans.AddNewlyCreatedDBObject(acCircCopy,True)

nCnt = nCnt + 1

End While

'' Save the changes and dispose of the transaction

acTrans.Commit()

End Using

End Sub

C#

using Autodesk.AutoCAD.Runtime;

using Autodesk.AutoCAD.ApplicationServices;

using Autodesk.AutoCAD.DatabaseServices;

using Autodesk.AutoCAD.Geometry;

using Autodesk.AutoCAD.Colors;

[CommandMethod("SetObjectColor")]

public static void SetObjectColor()

{

// Get the current document and database获取当前文档和数据库

Document acDoc = Application.DocumentManager.MdiActiveDocument;

Database acCurDb = acDoc.Database;

// Start a transaction启动事务

using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())

{

// Define an array of colors for the layers定义颜色数组

Color[] acColors = new Color[3];

acColors[0] = Color.FromColorIndex(ColorMethod.ByAci,1);

acColors[1] = Color.FromRgb(23,232);

acColors[2] = Color.FromNames("PANTONE Yellow 0131 C",

"PANTONE(R) pastel coated");

// Open the Block table for read以读打开块表

BlockTable acBlkTbl;

acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,

OpenMode.ForRead) as BlockTable;

// Open the Block table record Model space for write以写打开块表记录模型空间

BlockTableRecord acBlkTblRec;

acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],

OpenMode.ForWrite) as BlockTableRecord;

// Create a circle object and assign it the ACI value of 4创建一个圆并将其ACI值设为4

Point3d acPt = new Point3d(0,0);

Circle acCirc = new Circle();

acCirc.Center = acPt;

acCirc.Radius = 1;

acCirc.ColorIndex = 4;

acBlkTblRec.AppendEntity(acCirc);

acTrans.AddNewlyCreatedDBObject(acCirc,true);

int nCnt = 0;

while (nCnt < 3)

{

// Create a copy of the circle创建圆拷贝

Circle acCircCopy;

acCircCopy = acCirc.Clone() as Circle;

// Shift the copy along the Y-axis沿Y轴移动复本

acPt = new Point3d(acPt.X,acPt.Z);

acCircCopy.Center = acPt;

// Assign the new color to the circle给复本设置新颜色

acCircCopy.Color = acColors[nCnt];

acBlkTblRec.AppendEntity(acCircCopy);

acTrans.AddNewlyCreatedDBObject(acCircCopy,true);

nCnt = nCnt + 1;

}

// Save the changes and dispose of the transaction保存修改关闭事务

acTrans.Commit();

}

}

VBA/ActiveX Code Reference

Sub SetObjectColor()

' Define an array of colors

Dim colorObj(2) As New AcadAcCmColor

colorObj(0).ColorMethod = acColorMethodByACI

colorObj(0).ColorIndex = acRed

Call colorObj(1).SetRGB(23,232)

Call colorObj(2).SetColorBookColor("PANTONE(R) pastel coated",_

"PANTONE Yellow 0131 C")

' Define the center point of the circle

Dim centerPt(0 To 2) As Double

centerPt(0) = 0: centerPt(1) = 3: centerPt(2) = 0

' Create a new circle and assign it the ACI value of 4

Dim circleObj As AcadCircle

Set circleObj = ThisDrawing.ModelSpace.AddCircle(centerPt,1)

circleObj.color = acCyan

Dim nCnt As Integer

' Create 3 more circles

While (nCnt < 3)

' Create a copy of the circle

Dim circleObjCopy As AcadCircle

Set circleObjCopy = circleObj.Copy

' Shift the copy along the Y-axis

centerPt(1) = centerPt(1) + 3

circleObjCopy.Center = centerPt

' Assign the new color to the circle

circleObjCopy.TrueColor = colorObj(nCnt)

nCnt = nCnt + 1

Wend

End Sub

2.2、Make a color current through the database通过数据库设置当前颜色

This example sets a color current through the Database object with the Cecolor property.

本例通过Database对象的Cecolor属性将颜色设置为当前。

VB.NET

Imports Autodesk.AutoCAD.Runtime

Imports Autodesk.AutoCAD.ApplicationServices

Imports Autodesk.AutoCAD.DatabaseServices

Imports Autodesk.AutoCAD.Colors

<CommandMethod("SetColorCurrent")> _

Public Sub SetColorCurrent()

'' Get the current document

Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument

'' Set the current color

acDoc.Database.Cecolor = Color.FromColorIndex(ColorMethod.ByLayer,256)

End Sub

C#

using Autodesk.AutoCAD.Runtime;

using Autodesk.AutoCAD.ApplicationServices;

using Autodesk.AutoCAD.DatabaseServices;

using Autodesk.AutoCAD.Colors;

[CommandMethod("SetColorCurrent")]

public static void SetColorCurrent()

{

// Get the current document获取当前文档

Document acDoc = Application.DocumentManager.MdiActiveDocument;

// Set the current color设置当前颜色

acDoc.Database.Cecolor = Color.FromColorIndex(ColorMethod.ByLayer,256);

}

2.3、Make a color current with the CECOLOR system variable 使用系统变量CECOLOR设置当前颜色

This example sets the color Red current with the CECOLOR system variable.

本例使用系统变量CECOLOR将当前颜色设置为红色(Red)。

VB.NET

Application.SetSystemVariable("CECOLOR","1")

C#

Application.SetSystemVariable("CECOLOR","1");

VBA/ActiveX Code Reference

ThisDrawing.SetVariable "CECOLOR","1"

附:关于系统变量CECOLOR

(摘自《AutoCAD用户指南》)

类型: 字符串

保存位置: 图形

初始值: BYLAYER

功能:设置新对象的颜色。

有效值包括

1、BYLAYER 或 BYBLOCK ;

2、AutoCAD 颜色索引 (ACI):介于 1 和 255 之间的整数值,或前七种颜色中的一种颜色名称 (1 红、2 黄、3 绿、4 青、5 蓝、6 洋红、7 白/黑);

3、真彩色:介于 000 和 255 之间的 RGB 或 HSL 值,格式为“RGB:130,200,240”;

4、配色系统:标准 PANTONE 或自定义配色系统、DIC 颜色手册或 RAL 颜色集中的文字,例如“DIC COLOR GUIDE(R)$DIC 43”;

原文链接:https://www.f2er.com/vb/260879.html

猜你在找的VB相关文章