|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +''' |
| 3 | +/*--------------------------------------------------------------------------------------+ |
| 4 | +| $Copyright: (c) 2024 Bentley Systems, Incorporated. All rights reserved. $ |
| 5 | ++--------------------------------------------------------------------------------------*/ |
| 6 | +''' |
| 7 | + |
| 8 | +''' |
| 9 | +Running this sample needs to open GlassColor.dgn. |
| 10 | +This sample shows how to change the glass panels color from darker to lighter from the top row to bottom row. |
| 11 | +''' |
| 12 | + |
| 13 | +from MSPyBentley import * |
| 14 | +from MSPyBentleyGeom import * |
| 15 | +from MSPyECObjects import * |
| 16 | +from MSPyDgnPlatform import * |
| 17 | +from MSPyMstnPlatform import * |
| 18 | +from MSPyDgnView import * |
| 19 | +import random |
| 20 | + |
| 21 | + |
| 22 | +''' Check whether the element color need to be changed. ''' |
| 23 | +def needToChangeColor(msElement): |
| 24 | + # Element should be graphic, visible, not deleted and not construction |
| 25 | + if ((not msElement.ehdr.isGraphics) or msElement.hdr.dhdr.props.b.invisible |
| 26 | + or msElement.ehdr.deleted or msElement.hdr.dhdr.props.b.elementClass != 0): |
| 27 | + return False |
| 28 | + |
| 29 | + # Element should be on level "A-GLAZ" or "A-WALL-METL" |
| 30 | + ret1 = Level.GetIdFromName (ISessionMgr.ActiveDgnModelRef, 0, "A-GLAZ") |
| 31 | + if(ret1[0] != BentleyStatus.eSUCCESS): |
| 32 | + return False |
| 33 | + ret2 = Level.GetIdFromName (ISessionMgr.ActiveDgnModelRef, 0, "A-WALL-METL") |
| 34 | + if(ret2[0] != BentleyStatus.eSUCCESS): |
| 35 | + return False |
| 36 | + |
| 37 | + levelID1 = ret1[1] |
| 38 | + levelID2 = ret2[1] |
| 39 | + if(msElement.ehdr.level != levelID1 and msElement.ehdr.level != levelID2): |
| 40 | + return False |
| 41 | + |
| 42 | + return True |
| 43 | + |
| 44 | +def GetElementByXCoordinate(): |
| 45 | + ACTIVEMODEL = ISessionMgr.ActiveDgnModelRef |
| 46 | + dgnModel = ACTIVEMODEL.GetDgnModel() |
| 47 | + graphicalElements = dgnModel.GetGraphicElements() |
| 48 | + categorizedElementsByX = {} |
| 49 | + for elementRef in graphicalElements: |
| 50 | + eeh = EditElementHandle(elementRef, dgnModel) |
| 51 | + msElement = eeh.GetElement () |
| 52 | + |
| 53 | + if(not needToChangeColor(msElement)): |
| 54 | + continue |
| 55 | + |
| 56 | + # Categorize elements by z-coordinate |
| 57 | + xCoordinate = msElement.hdr.dhdr.range.xlowlim |
| 58 | + if round(xCoordinate/10000000) not in categorizedElementsByX: |
| 59 | + categorizedElementsByX[round(xCoordinate/10000000)] = [] |
| 60 | + categorizedElementsByX[round(xCoordinate/10000000)].append(elementRef) |
| 61 | + |
| 62 | + return categorizedElementsByX |
| 63 | + |
| 64 | +''' Change the glass panels color ''' |
| 65 | +def ChangeGlassWallColor(graphicalElements): |
| 66 | + ACTIVEMODEL = ISessionMgr.ActiveDgnModelRef |
| 67 | + dgnModel = ACTIVEMODEL.GetDgnModel() |
| 68 | + categorizedElements = {} |
| 69 | + for elementRef in graphicalElements: |
| 70 | + eeh = EditElementHandle(elementRef, dgnModel) |
| 71 | + msElement = eeh.GetElement () |
| 72 | + |
| 73 | + if(not needToChangeColor(msElement)): |
| 74 | + continue |
| 75 | + |
| 76 | + # Categorize elements by z-coordinate |
| 77 | + zCoordinate = msElement.hdr.dhdr.range.zlowlim |
| 78 | + if zCoordinate not in categorizedElements: |
| 79 | + categorizedElements[zCoordinate] = [] |
| 80 | + categorizedElements[zCoordinate].append(elementRef) |
| 81 | + |
| 82 | + |
| 83 | + color = 80 |
| 84 | + transparency = 0 |
| 85 | + # Change glass panels color for each row |
| 86 | + for zCoordinate in sorted(categorizedElements, reverse=True): |
| 87 | + for elementRef in categorizedElements[zCoordinate]: |
| 88 | + eeh = EditElementHandle(elementRef, dgnModel) |
| 89 | + propertiesSetter = ElementPropertiesSetter() |
| 90 | + propertiesSetter.SetColor(round(color)) |
| 91 | + propertiesSetter.SetTransparency(transparency) |
| 92 | + if True == propertiesSetter.Apply(eeh): |
| 93 | + eeh.ReplaceInModel(elementRef) |
| 94 | + |
| 95 | + if (len(categorizedElements) > 1): |
| 96 | + color += 15/(len(categorizedElements) - 1) |
| 97 | + transparency += 0.8/(len(categorizedElements) - 1) |
| 98 | + |
| 99 | +def ChangeGlassColor(): |
| 100 | + categorizedElementsByX = GetElementByXCoordinate() |
| 101 | + for xCoordinate in sorted(categorizedElementsByX, reverse=True): |
| 102 | + ChangeGlassWallColor(categorizedElementsByX[xCoordinate]) |
| 103 | + |
| 104 | +if __name__ == "__main__": |
| 105 | + ChangeGlassColor() |
| 106 | + |
| 107 | + |
0 commit comments