def ReadList(self,li): """Read from a python list. list should be 2 dimension. [(A11,A12,...Am), (A21,A22,...Am), ... (An1,An2,...Anm)] """ self.Cells=[] self.NColumn=len(li[0]) self.NRow=len(li) for i in range(self.NColumn): for j in range(self.NRow): c=Cell() c.M=i+1 c.N=j+1 c.String=li[j][i] self.Cells.append(c) #caculate the position dx=0 #x offset of cells for i in range(self.NColumn): column=self.GetColumn(i+1) maxLength=max([kcs_draft.text_length(c.GetText()) for c in column])+1 for c in column: cor1=Point2D(self.Location.X+dx,self.Location.Y-self.RowHeight*c.N) cor2=Point2D(cor1.X+maxLength,cor1.Y+self.RowHeight) c.Rect.SetCorners(cor1,cor2) dx+=maxLength
def GetRegion(): # 获得当前图纸最大范围 # 获得所有View viewList = [] try: view = kcs_draft.element_child_first_get() viewList.append(view) while True: try: view = kcs_draft.element_sibling_next_get(view) viewList.append(view) except: break except: # 没有找到任何视图 return None rect = kcs_draft.element_extent_get(viewList[0]) for view in viewList[1:]: rect2 = kcs_draft.element_extent_get(view) rect.SetCorner1(Point2D(min(rect.Corner1.X, rect2.Corner1.X), min(rect.Corner1.Y, rect2.Corner1.Y))) rect.SetCorner2(Point2D(max(rect.Corner2.X, rect2.Corner2.X), max(rect.Corner2.Y, rect2.Corner2.Y))) region = CaptureRegion2D() region.SetRectangle(rect) region.Inside = 1 region.Cut = 0 return region
def Grid(): """Create a grid on a new view""" res=kcs_ui.string_req('请输入格子的长,宽、列数,行数','30,10,8,5') if res[0]==kcs_util.ok(): w,h,m,n=[float(i) for i in res[1].split(',')] pt=Point2D() res=kcs_ui.point2D_req('请选择Grid左上角位置,回车设为原点',pt) if res[0]==kcs_util.ok(): csm=CurrentSubpictureManager() hd=kcs_draft.view_new('') csm.Set(hd) for i in range(n+1): #horizontal line p1=Point2D(pt.X,pt.Y-h*i) p2=Point2D(pt.X+w*m,pt.Y-h*i) kcs_draft.line_new(Rline2D(p1,p2)) for j in range(m+1): #vertical line p1=Point2D(pt.X+j*w,pt.Y) p2=Point2D(pt.X+j*w,pt.Y-h*n) kcs_draft.line_new(Rline2D(p1,p2)) csm.Back() elif res[0]==kcs_util.operation_complete(): pt=Point2D(0,0) csm=CurrentSubpictureManager() hd=kcs_draft.view_new('') csm.Set(hd) for i in range(n+1): #horizontal line p1=Point2D(pt.X,pt.Y-h*i) p2=Point2D(pt.X+w*m,pt.Y-h*i) kcs_draft.line_new(Rline2D(p1,p2)) for j in range(m+1): #vertical line p1=Point2D(pt.X+j*w,pt.Y) p2=Point2D(pt.X+j*w,pt.Y-h*n) kcs_draft.line_new(Rline2D(p1,p2)) csm.Back()
def ZoomElement(element): try: rect = kcs_draft.element_extent_get(element) rect.SetCorner1(Point2D(rect.Corner1.X - 100, rect.Corner1.Y - 100)) rect.SetCorner2(Point2D(rect.Corner2.X + 100, rect.Corner2.Y + 100)) kcs_draft.dwg_zoom(rect) except: kcs_ui.message_noconfirm('Failed to zoom text')
def Diamond(center,width,height): """ center:center point. width:x direction length height:y direction length """ dx=width*1.0/2 dy=height*1.0/2 p1=Point2D(center.X+dx,center.Y) p2=Point2D(center.X,center.Y-dy) p3=Point2D(center.X-dx,center.Y) p4=Point2D(center.X,center.Y+dy) DrawLines([p1,p2,p3,p4])
def SetUnderline(): "add underline for text" hdText=SelectText() if hdText!=None: csm=ktDraft.CurrentSubpictureManager() rect=kcs_draft.element_extent_get(hdText) #begin to draw underline x1=min(rect.Corner1.X,rect.Corner2.X) x2=max(rect.Corner1.X,rect.Corner2.X) y=min(rect.Corner1.Y,rect.Corner2.Y)-1 csm.Set(hdText) line=Rline2D(Point2D(x1,y),Point2D(x2,y)) kcs_draft.line_new(line) csm.Back()
def PrintColors(): colors=KcsColour.ColourStrings pos=0 for i in range(len(colors)): col=KcsColour.Colour(colors[i]) kcs_draft.colour_set(col) p1=Point2D(0,pos) p2=Point2D(100,pos) ln=Rline2D(p1,p2) kcs_draft.line_new(ln) kcs_draft.text_new(colors[i],Point2D(p2.X+20,p2.Y)) pos+=20
def SelectTexts(): cor1 = Point2D() cor2 = Point2D() res = kcs_ui.point2D_req("请选择区域左上角", cor1) if res[0] == kcs_util.ok(): res = kcs_ui.point2D_req("请选择区域右下角", cor2) if res[0] == kcs_util.ok(): rect = Rectangle2D(cor1, cor2) region = CaptureRegion2D() region.SetRectangle(rect) region.Inside = 1 region.Cut = 1 hds = kcs_draft.text_capture(region) return hds return
def Point3DCoord(): "show the coordinate of selected point" pt = Point2D() res = kcs_ui.point2D_req("请选择要查询的点", pt) if res[0] == kcs_util.ok(): res = kcs_util.tra_coord_ship(pt.X, pt.Y, "") if res[0] == 0: pt3d = Point3D(res[1], res[2], res[3]) res, fr, fr_offset = kcs_util.coord_to_pos(1, pt3d.X) res, lp_y, lp_y_offset = kcs_util.coord_to_pos(2, pt3d.Y) res, lp_z, lp_z_offset = kcs_util.coord_to_pos(3, pt3d.Z) fr_offset=round(fr_offset,2) lp_y_offset=round(lp_y_offset,2) lp_z_offset=round(lp_z_offset,2) x=round(pt3d.X,2) y=round(pt3d.Y,2) z=round(pt3d.Z,2) Msg("--"*25) Msg("二维点:" + str(pt)) Msg("三维坐标信息:") Msg("X: %s,FR%s %s" % (string.ljust(str(x), width), str(fr), _fmt(fr_offset))) Msg("Y: %s,LP%s %s" % (string.ljust(str(y), width), str(lp_y), _fmt(lp_y_offset))) Msg("Z: %s,LP%s %s" % (string.ljust(str(z), width), str(lp_z), _fmt(lp_z_offset))) Msg("--"*25)
def ArcTo(self,x,y,dz=1): "create arc to given x and y" end=Point2D(x,y) amp=self.Sta.DistanceToPoint(end)/2 arc=Arc2D(self.Sta,end,amp*dz) kcs_draft.arc_new(arc) self.Sta=end
def PrintLinetypes(): tps = KcsLinetype.GetLinetypes() pos = 0 for i in range(len(tps)): msg(i) tp = tps.keys()[i] name = KcsLinetype.GetSystemName(tp) lt = KcsLinetype.Linetype(name) kcs_draft.linetype_set(lt) p1 = Point2D(0, pos) p2 = Point2D(100, pos) ln = Rline2D(p1, p2) kcs_draft.line_new(ln) kcs_draft.text_new(name, Point2D(p2.X + 20, p2.Y)) pos += 20
def PrintUserResponse(): while True: pt = Point2D() res = kcs_ui.point2D_req('Esc to quit.', pt) if res[0] == util.cancel(): break else: msg('Your response is: %s' % res[0])
def CreateTable(li): """create table from a python list""" pt=Point2D() res=kcs_ui.point2D_req('Please pick the left-top corner of table.',pt) if res[0]==kcs_util.ok(): t=Table() t.ReadList(li) t.Draw()
def GetText(self): cor=self.Rect.Corner1 txt=Text(self.String) txt.SetPosition(Point2D(cor.X+1,cor.Y+1)) txt.SetColour(Colour("Black")) txt.SetFont("ΊΪΜε") txt.SetHeight(2.5) return txt
def __init__(self): self.Location = Point2D(0, 0) self.Width = 420 self.Height = 297 self.PaddingLeft = 20 self.PaddingRight = 5 self.PaddingTop = 5 self.PaddingBottom = 5 self.Index = 0
def SelectText(): "select a text from drawing" pt=Point2D() res=kcs_ui.point2D_req('请选择文字',pt) if res[0]==kcs_util.ok(): try: hdText=kcs_draft.text_identify(res[1]) return hdText except: return None
def Arrow(start,end,size=2.5,angle=15): #distance can't less than size if start.DistanceToPoint(end)<=size: raise 'Given points is too close.' length=start.DistanceToPoint(end) scale=(length-size)/size x=(start.X+scale*end.X)/(1+scale) y=(start.Y+scale*end.Y)/(1+scale) theta=GetAngle(start, end) d=size*math.tan(math.radians(angle)) dx=d*math.sin(theta) dy=d*math.cos(theta) p3=Point2D(x+dx,y-dy) p4=Point2D(x-dx,y+dy) kcs_draft.line_new(Rline2D(start, end)) kcs_draft.line_new(Rline2D(p3, end)) kcs_draft.line_new(Rline2D(p4, end)) kcs_draft.line_new(Rline2D(p3, p4))
def Init(self,m,n,width=30,pos=Point2D(0,0)): """init a empty m x n table. m:column count n:row count width:width of each column. """ self.Cells=[] self.NColumn=m self.NRow=n self.Location=pos for i in range(self.NColumn): for j in range(self.NRow): c=Cell() c.M=i+1 c.N=j+1 c.String='' c.Rect.SetCorner1(Point2D(i*width+self.Location.X,-(j+1)*self.RowHeight+self.Location.Y)) c.Rect.SetCorner2(Point2D((i+1)*width+self.Location.X,-j*self.RowHeight+self.Location.Y)) self.Cells.append(c)
def __init__(self,hd=None): "init by view handle" self.NRow=0 #count of row.(n) self.NColumn=0 #count of column.(m) self.Cells=[] self.Handle=hd #handle of view self.Location=Point2D(0,0) #left top corner of table self.RowHeight=3.8 if hd!=None: self._set(hd)
def run(): stat = KcsStat_point2D_req.Stat_point2D_req() ct = CursorType() CursorTypes = { 'CrossHair': 1, 'RubberBand': 2, 'RubberRectangle': 3, 'RubberCircle': 4, 'DragCursor': 5 } actions = CursorTypes.keys() bs = ButtonState() bs.EnableLock(1) bs.SetCheckedLock('V') pt = Point2D(2, 3) while True: res = kcs_ui.choice_select('title', 'header', actions) if res[0] == kcs_util.ok(): #msg("%s:%s"%(res[1],CursorTypes.keys()[res[1]-1])) if res[1] == 1: ct.SetCrossHair() elif res[1] == 2: ct.SetRubberBand(pt) elif res[1] == 3: ct.SetRubberRectangle(pt) elif res[1] == 4: ct.SetRubberCircle(pt) elif res[1] == 5: pass stat.SetCursorType(ct) stat.SetDefMode('ModeMidPoint') stat.SetHelpPoint(Point2D(0, 0)) msg(ct) res, pt2 = kcs_ui.point2D_req('msg', pt, stat, bs) msg(pt2) else: break
def run(): pt = Point2D() res = kcs_ui.point2D_req('请选择中心点', pt) if res[0] == kcs_util.ok(): res2 = kcs_ui.string_req('请输入菱形的尺寸', '300,100') if res2[0] == kcs_util.ok(): li = [float(s) for s in res2[1].split(',')] if len(li) == 2: a, b = li else: raise 'wrong input' ktBasic.Diamond(res[1], a, b)
def NewArrow(start, end, size=2.5, angle=15): """画水平箭头""" arrowHeight = math.tan(angle * math.pi / 180) * size line = Rline2D(start, end) kcs_draft.line_new(line) pt = Point2D() pt.X = end.X - size pt.Y = end.Y + arrowHeight line = Rline2D(end, pt) kcs_draft.line_new(line) pt2 = Point2D() pt2.X = pt.X pt2.Y = pt.Y - 2 * arrowHeight line = Rline2D(pt, pt2) kcs_draft.line_new(line) line = Rline2D(pt2, end) kcs_draft.line_new(line)
def DeleteNote(): pt=Point2D() res=kcs_ui.point2D_req("请选择要删除的批注",pt) if res[0]==kcs_util.ok(): try: hText=kcs_draft.text_identify(pt) comp=kcs_draft.element_parent_get(hText) compName=kcs_draft.subpicture_name_get(comp) if compName.startswith("NOTE_"): kcs_draft.element_delete(comp) except Exception,e: kcs_ui.message_noconfirm(e.message)
def ChangeColumnText(): pt=Point2D() res=kcs_ui.point2D_req('Please select the column',pt) if res[0]==kcs_util.ok(): hdView=kcs_draft.view_identify(res[1]) comp=kcs_draft.component_identify(res[1]) #get column index from comp name name=kcs_draft.subpicture_name_get(comp) colIndex=int(name[2:].split('X')[0]) table=Table(hdView) res=kcs_ui.string_req('Please enter the string','iknot') if res[0]==kcs_util.ok(): table.ChangeColumnText(colIndex, res[1])
def MoveColumn(): pt=Point2D() res=kcs_ui.point2D_req('Please select the column',pt) if res[0]==kcs_util.ok(): hdView=kcs_draft.view_identify(res[1]) comp=kcs_draft.component_identify(res[1]) #get column index from comp name name=kcs_draft.subpicture_name_get(comp) colIndex=int(name[2:].split('X')[0]) table=Table(hdView) res=kcs_ui.int_req('Please enter the offset',100) if res[0]==kcs_util.ok(): table.MoveColumn(colIndex, res[1])
def ViewToRect(viewHandle, rectangle, fixScale=True): """将视图缩放到给定矩形内 Return: scale""" viewRec = kcs_draft.element_extent_get(viewHandle) scale_x = abs((viewRec.Corner2.X - viewRec.Corner1.X) / (rectangle.Corner2.X - rectangle.Corner1.X)) scale_y = abs((viewRec.Corner2.Y - viewRec.Corner1.Y) / (rectangle.Corner2.Y - rectangle.Corner1.Y)) scale = max(scale_x, scale_y) if fixScale: # 修改比例(50,75,100) if scale < 50: scale = 50.0 elif scale < 75: scale = 75.0 elif scale < 100: scale = 100.0 kcs_draft.view_scale(viewHandle, 1.0 / scale, viewRec.Corner1) # 移动到页面中心位置 viewRec = kcs_draft.element_extent_get(viewHandle) viewCenter = Point2D((viewRec.Corner1.X + viewRec.Corner2.X) / 2, (viewRec.Corner1.Y + viewRec.Corner2.Y) / 2) recCenter = Point2D((rectangle.Corner1.X + rectangle.Corner2.X) / 2, (rectangle.Corner1.Y + rectangle.Corner2.Y) / 2) moveVec = Vector2D(recCenter.X - viewCenter.X, recCenter.Y - viewCenter.Y) kcs_draft.view_move(viewHandle, moveVec) return scale
def FormatSingle(): pt = Point2D() res = kcs_ui.point2D_req("请选择文字获取文字属性", pt) if res[0] == kcs_util.ok(): hd = kcs_draft.text_identify(pt) ori = Text() kcs_draft.text_properties_get(hd, ori) # old = kcs_draft.subpicture_current_get() # 当前图层 while True: "select single text" pt2=Point2D() res = kcs_ui.point2D_req("请选择要替换的文字", pt2) if res[0]==kcs_util.ok(): hdText=kcs_draft.text_identify(pt2) father = kcs_draft.element_parent_get(hdText) # 文字所在图层 t = Text() kcs_draft.text_properties_get(hdText, t) ori.SetString(t.GetString()) ori.SetPosition(t.GetPosition()) kcs_draft.element_delete(hdText) # 删除原文字 kcs_draft.subpicture_current_set(father) # 重设图层 kcs_draft.text_new(ori) # 写文字 else: break
def run(): pt = Point2D() res = kcs_ui.point2D_req('请选择中心点', pt) if res[0] == kcs_util.ok(): res2 = kcs_ui.string_req('请输入外接圆半径、数量和旋转角度(默认为0)', '100,6,0') if res2[0] == kcs_util.ok(): li = [float(s) for s in res2[1].split(',')] radius, number, angle = 0.0, 0.0, 0.0 if len(li) == 2: radius, number = li elif len(li) == 3: radius, number, angle = li else: raise 'wrong input' ktBasic.Polygon(res[1], radius, number, angle)
def CreateEmptyTable(): """ Create an m x n table without text. """ res=kcs_ui.string_req('please enter the column number,row number and the width of each cell', '5,3,30') if res[0]==kcs_util.ok(): li=res[1].split(',') m,n,width=int(li[0]),int(li[1]),float(li[2]) pt=Point2D() res=kcs_ui.point2D_req('Please pick the left-top corner of table.',pt) if res[0]==kcs_util.ok(): t=Table() t.Init(m, n, width, res[1]) t.Draw()
def CreateForm(): "create a form with n pages." #check whether view exist. ViewName = 'KT_FORM' for view in ktDraft.GetViews(): if view[1] == 'KT_FORM': print 'view already exist.' return hd = kcs_draft.view_new(ViewName) res = kcs_ui.int_req("How many pages do you want?", 1) if res[0] == kcs_util.ok(): for i in range(res[1]): pg = Page() pg.Location = Point2D(i * 420, 0) pg.Index = i + 1 kcs_draft.subpicture_current_set(hd) pg.Draw() kcs_draft.dwg_purge()