乐筑天下

搜索
欢迎各位开发者和用户入驻本平台 尊重版权,从我做起,拒绝盗版,拒绝倒卖 签到、发布资源、邀请好友注册,可以获得银币 请注意保管好自己的密码,避免账户资金被盗
查看: 8|回复: 8

[编程交流] Trying to color lines - autoca

[复制链接]

33

主题

107

帖子

65

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
175
发表于 2022-7-6 11:00:05 | 显示全部楼层 |阅读模式
Hello: I want to modify the color of some lines I created earlier in my subroutine, but it's not working.
 
Earlier in the sub, I call DrawLines to draw the lines (working fine):
 
  1. Public Shared Function DrawLine(ByVal pStart, ByVal pEnd, ByVal sColor) As Line           Dim myTransMan As Autodesk.AutoCAD.DatabaseServices.TransactionManager           Dim myTrans As Autodesk.AutoCAD.DatabaseServices.Transaction           Dim myDWG As Autodesk.AutoCAD.ApplicationServices.Document           Dim myBT As Autodesk.AutoCAD.DatabaseServices.BlockTable           Dim myBTR As Autodesk.AutoCAD.DatabaseServices.BlockTableRecord           'Get the active document and begin a Transaction           myDWG = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument           myTransMan = myDWG.TransactionManager           myTrans = myTransMan.StartTransaction           'Open the BlockTable for Read           myBT = myDWG.Database.BlockTableId.GetObject( _               Autodesk.AutoCAD.DatabaseServices.OpenMode.ForRead)           myBTR = myBT(Autodesk.AutoCAD.DatabaseServices.BlockTableRecord.ModelSpace).GetObject( _               Autodesk.AutoCAD.DatabaseServices.OpenMode.ForWrite)           'Draw the line           Dim myLine As New Autodesk.AutoCAD.DatabaseServices.Line(pStart, pEnd)           'was this line already just drawn?            Select Case sColor               Case "YellowGreen"                   myLine.Color = Autodesk.AutoCAD.Colors.Color.FromColor(Drawing.Color.YellowGreen)               Case "Pink"                   myLine.Color = Autodesk.AutoCAD.Colors.Color.FromColor(Drawing.Color.Pink)               Case "Purple"                   myLine.Color = Autodesk.AutoCAD.Colors.Color.FromColor(Drawing.Color.Purple)               Case "Blue"                   myLine.Color = Autodesk.AutoCAD.Colors.Color.FromColor(Drawing.Color.Blue)               Case "Coral"                   myLine.Color = Autodesk.AutoCAD.Colors.Color.FromColor(Drawing.Color.Coral)                   'small lines used just to see if the merge is correct - temp lines               Case "Gold"                   myLine.Color = Autodesk.AutoCAD.Colors.Color.FromColor(Drawing.Color.Gold)               Case "Red"                   myLine.Color = Autodesk.AutoCAD.Colors.Color.FromColor(Drawing.Color.Red)           End Select           myLine.Layer = 0           myBTR.AppendEntity(myLine)           myTrans.AddNewlyCreatedDBObject(myLine, True)           DrawLine = myLine           'Commit the Transaction           myTrans.Commit()           myTrans.Dispose()           myTransMan.Dispose()       End Function
 
Later, I want to modify the color of the lines I drew, so i try to do something link this:
 
  1. For m As Integer = 0 To myListOfEdges.Count - 1                               Edge = myListOfEdges.Item(m)                               Debug.Print("edge: " & Edge.sTypeEdge.ToString())                               Debug.Print("line len: " & Edge.dTotalLengthOfLines.ToString())                               If Not IsNothing(Edge.lstLinesInEdge) And Edge.sTypeEdge.ToString() = "secondary" Then                                   For j As Integer = 0 To Edge.lstLinesInEdge.Count - 1                                       Dim oLine As Line = Edge.lstLinesInEdge(j)                                       Debug.Print("line len: " & oLine.Length.ToString())'main line that attempts to change color of line:      [color=red]oLine.Color = Autodesk.AutoCAD.Colors.Color.FromColor(Drawing.Color.Green)[/color]                                   Next                               End If                           Nexttr.commit
 
After the subroutine finishes and I can see the drawing in cad, the color of the lines didn't change. I try doing a regen to refresh the screen, but the color of the lines still haven't changed.
 
I'm wondering what am i missing?
 
Thanks,
Proctor
回复

使用道具 举报

13

主题

146

帖子

136

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
62
发表于 2022-7-6 11:09:13 | 显示全部楼层
You might have to use color numbers instead of color names.
chroma.dwg
回复

使用道具 举报

33

主题

107

帖子

65

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
175
发表于 2022-7-6 11:20:37 | 显示全部楼层
Hi StevJ: Thanks for your reply. I went to try that; however, the intellisense for the FromColor() method pops up and has me pick out
a Drawing.Color from it's list so I believe that parts ok.

Thanks again for your help,
Proctor
回复

使用道具 举报

10

主题

973

帖子

909

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
118
发表于 2022-7-6 11:24:47 | 显示全部楼层
Is the Line contained within Edge.lstLinesInEdge(j) already database resident.  If so then it would typically need to be OpenMode.ForWrite before any of it’s properties can be changed.
 
I suppose it is not impossible to have a collection of “Open” database resident objects, but that sound a bit risky. Does the routine’s objective require dealing with entities in that fashion?
回复

使用道具 举报

106

主题

1万

帖子

101

银币

顶梁支柱

Rank: 50Rank: 50

铜币
1299
发表于 2022-7-6 11:31:54 | 显示全部楼层
I agree with stevj if your going to play around with colours its a lot easier to use the numbers then you don't have to strart remembering two different colour systems AcRed v's 1   AcYellow v's 2 also when you start using more than color 9 its easier to remember 20 32 50 140 (we plot in b&w plus colour) red orange yellow lightblue
回复

使用道具 举报

33

主题

107

帖子

65

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
175
发表于 2022-7-6 11:39:22 | 显示全部楼层
Thank you all for your help. I hope you don't mind but, I think I'd like to back the truck up a bit...because I'm now thinking that perhaps I'm confused in regards to what exactly needs to happen when creating lines.
 
1) My code starts asks the user to select a poly and then it locks the document and starts a transaction:
 
  1. If Not IsNothing(myPSR.Value) Then                   Using docLock As DocumentLock = myEd.Document.LockDocument()                       Using tr As Transaction = myDB.TransactionManager.StartTransaction()
 
2) It then loops through each of the poly vertecies and creates lines:
  1. For i = 0 To pPoly.NumberOfVertices - 1   If i > 0 Then         myLine = New Line(myPt, pPoly.GetPoint3dAt(i))   End ifnext
 
I add these lines into a collection to be used later;
At some point in my sub, I then want to display one of the lines in my collection.
 
Originally, I was uncertain how to display the line....so I called my DrawLine routine. it takes in the line's start and end points as a parameter and is basically creating the line. but, isn't the line already created? This was the only way i was able to get the line to show up and now that I'm thinking about it....i don't think this is right. I think there must be some how to display the line created originally in my code (using code under #2).
 
Thanks for your help....
Proctor
回复

使用道具 举报

33

主题

107

帖子

65

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
175
发表于 2022-7-6 11:49:27 | 显示全部楼层
ok..i just read that in order to draw the line ...when it's created, you need to add it to the block table record. so now, as i'm looping through and creating each line, I'm adding it to the block table record:
 
  1. [size=2][color=#0000ff][size=2][color=#0000ff]For[/color][/size][/color][/size][size=2] i = 0 [/size][size=2][color=#0000ff][size=2][color=#0000ff]To[/color][/size][/color][/size][size=2] pPoly.NumberOfVertices - 1[/size][size=2][color=#0000ff][size=2][/size][/color][/size][size=2]myLine = [/size][size=2][color=#0000ff][size=2][color=#0000ff]New[/color][/size][/color][/size][size=2] Line(myPt, pPoly.GetPoint3dAt(i))[/size][size=2]CreateLine(myLine, [/size][size=2][color=#800000][size=2][color=#800000]"Red"[/color][/size][/color][/size][size=2])[/size]
 
i modified my DrawLine so that i can pass in the line just created and add it to the btr:
 
  1. [size=2][color=#0000ff][size=2][color=#0000ff]Public[/color][/size][/color][/size][size=2][color=#0000ff][size=2][color=#0000ff]Shared[/color][/size][/color][/size][size=2][color=#0000ff][size=2][color=#0000ff]Function[/color][/size][/color][/size][size=2] CreateLine([/size][size=2][color=#0000ff][size=2][color=#0000ff]ByVal[/color][/size][/color][/size][size=2] myLine, [/size][size=2][color=#0000ff][size=2][color=#0000ff]ByVal[/color][/size][/color][/size][size=2] sColor) [/size][size=2][color=#0000ff][size=2][color=#0000ff]As[/color][/size][/color][/size][size=2] Line[/size][size=2][color=#0000ff][size=2][color=#0000ff]Dim[/color][/size][/color][/size][size=2] myTransMan [/size][size=2][color=#0000ff][size=2][color=#0000ff]As[/color][/size][/color][/size][size=2] Autodesk.AutoCAD.DatabaseServices.TransactionManager[/size][size=2][color=#0000ff][size=2][color=#0000ff]Dim[/color][/size][/color][/size][size=2] myTrans [/size][size=2][color=#0000ff][size=2][color=#0000ff]As[/color][/size][/color][/size][size=2] Autodesk.AutoCAD.DatabaseServices.Transaction[/size][size=2][color=#0000ff][size=2][color=#0000ff]Dim[/color][/size][/color][/size][size=2] myDWG [/size][size=2][color=#0000ff][size=2][color=#0000ff]As[/color][/size][/color][/size][size=2] Autodesk.AutoCAD.ApplicationServices.Document[/size][size=2][color=#0000ff][size=2][color=#0000ff]Dim[/color][/size][/color][/size][size=2] myBT [/size][size=2][color=#0000ff][size=2][color=#0000ff]As[/color][/size][/color][/size][size=2] Autodesk.AutoCAD.DatabaseServices.BlockTable[/size][size=2][color=#0000ff][size=2][color=#0000ff]Dim[/color][/size][/color][/size][size=2] myBTR [/size][size=2][color=#0000ff][size=2][color=#0000ff]As[/color][/size][/color][/size][size=2] Autodesk.AutoCAD.DatabaseServices.BlockTableRecord[/size][size=2][color=#008000][size=2][color=#008000]'Get the active document and begin a Transaction[/color][/size][/color][/size][size=2]myDWG = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocumentmyTransMan = myDWG.TransactionManagermyTrans = myTransMan.StartTransaction[/size][size=2][color=#008000][size=2][color=#008000]'Open the BlockTable for Read[/color][/size][/color][/size][size=2]myBT = myDWG.Database.BlockTableId.GetObject( _Autodesk.AutoCAD.DatabaseServices.OpenMode.ForRead)myBTR = myBT(Autodesk.AutoCAD.DatabaseServices.BlockTableRecord.ModelSpace).GetObject( _Autodesk.AutoCAD.DatabaseServices.OpenMode.ForWrite)[/size][size=2][color=#008000][size=2][/size][/color][/size][size=2][color=#0000ff][size=2][color=#0000ff]Select[/color][/size][/color][/size][size=2][color=#0000ff][size=2][color=#0000ff]Case[/color][/size][/color][/size][size=2] sColor[/size][size=2][color=#0000ff][size=2][color=#0000ff]Case[/color][/size][/color][/size][size=2][color=#800000][size=2][color=#800000]"YellowGreen"[/color][/size][/color][/size][size=2]myLine.Color = Autodesk.AutoCAD.Colors.Color.FromColor(Drawing.Color.YellowGreen)[/size][size=2][color=#0000ff][size=2][color=#0000ff]Case[/color][/size][/color][/size][size=2][color=#800000][size=2][color=#800000]"Pink"[/color][/size][/color][/size][size=2]myLine.Color = Autodesk.AutoCAD.Colors.Color.FromColor(Drawing.Color.Pink)[/size][size=2][color=#0000ff][size=2][color=#0000ff]Case[/color][/size][/color][/size][size=2][color=#800000][size=2][color=#800000]"Purple"[/color][/size][/color][/size][size=2]myLine.Color = Autodesk.AutoCAD.Colors.Color.FromColor(Drawing.Color.Purple)[/size][size=2][color=#0000ff][size=2][color=#0000ff]Case[/color][/size][/color][/size][size=2][color=#800000][size=2][color=#800000]"Blue"[/color][/size][/color][/size][size=2]myLine.Color = Autodesk.AutoCAD.Colors.Color.FromColor(Drawing.Color.Blue)[/size][size=2][color=#0000ff][size=2][color=#0000ff]Case[/color][/size][/color][/size][size=2][color=#800000][size=2][color=#800000]"Coral"[/color][/size][/color][/size][size=2]myLine.Color = Autodesk.AutoCAD.Colors.Color.FromColor(Drawing.Color.Coral)[/size][size=2][color=#008000][size=2][color=#008000]'small lines used just to see if the merge is correct - temp lines[/color][/size][/color][/size][size=2][color=#0000ff][size=2][color=#0000ff]Case[/color][/size][/color][/size][size=2][color=#800000][size=2][color=#800000]"Gold"[/color][/size][/color][/size][size=2]myLine.Color = Autodesk.AutoCAD.Colors.Color.FromColor(Drawing.Color.Gold)[/size][size=2][color=#0000ff][size=2][color=#0000ff]Case[/color][/size][/color][/size][size=2][color=#800000][size=2][color=#800000]"Red"[/color][/size][/color][/size][size=2]myLine.Color = Autodesk.AutoCAD.Colors.Color.FromColor(Drawing.Color.Red)[/size][size=2][color=#0000ff][size=2][color=#0000ff]End[/color][/size][/color][/size][size=2][color=#0000ff][size=2][color=#0000ff]Select[/color][/size][/color][/size][size=2]myLine.Layer = 0myBTR.AppendEntity(myLine)myTrans.AddNewlyCreatedDBObject(myLine, [/size][size=2][color=#0000ff][size=2][color=#0000ff]True[/color][/size][/color][/size][size=2])CreateLine = myLine[/size][size=2][color=#008000][size=2][color=#008000]'Commit the Transaction[/color][/size][/color][/size][size=2]myTrans.Commit()myTrans.Dispose()myTransMan.Dispose()[/size][size=2][color=#0000ff][size=2][color=#0000ff]End[/color][/size][/color][/size][size=2][color=#0000ff][size=2][color=#0000ff]Function[/color][/size][/color][/size]
 
now, I'm going to try to change the line's color...I'll get back to you shortly...I just wanted to let you know where I'm at now.
 
thanks,
Proctor
回复

使用道具 举报

33

主题

107

帖子

65

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
175
发表于 2022-7-6 11:55:23 | 显示全部楼层
Well..that's what the issue was..bad code. now that i'm creating the line and adding it to the btr, i'm able to change the color using:
 
colListOfLines(t).Color = Autodesk.AutoCAD.Colors.Color.FromColor(Drawing.Color.Blue)
 
thanks again for helping me.
Proctor
回复

使用道具 举报

0

主题

127

帖子

130

银币

限制会员

铜币
-2
发表于 2022-7-6 12:00:35 | 显示全部楼层
Proctor,
a couple of things ..
Have you read the AutoCAD .NET Developer's Guide
http://docs.autodesk.com/ACD/2010/ENU/AutoCAD%20.NET%20Developer's%20Guide/index.html
 
Have a look at Use Transactions with the Transaction Manager
 
and
 
Consider making better use of namespaces instead of doing so much typing
ie if you have used
Imports Autodesk.AutoCAD.DatabaseServices
OR for C#
using Autodesk.AutoCAD.DatabaseServices
 
then this
Autodesk.AutoCAD.DatabaseServices.BlockTableRecord.ModelSpace
can become
BlockTableRecord.ModelSpace
 
at the end of the day, the code is easier to read and thats half the battle when starting.
回复

使用道具 举报

发表回复

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

QQ|关于我们|小黑屋|乐筑天下 繁体中文

GMT+8, 2025-3-6 11:43 , Processed in 0.902713 second(s), 70 queries .

© 2020-2025 乐筑天下

联系客服 关注微信 帮助中心 下载APP 返回顶部 返回列表