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 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 SetRectangle(): "Add a rectangle around text" hdText=SelectText() if hdText!=None: csm=ktDraft.CurrentSubpictureManager() csm.Set(hdText) rect=kcs_draft.element_extent_get(hdText) rect.Corner1.X-=1 rect.Corner1.Y-=1 rect.Corner2.X+=1 rect.Corner2.Y+=1 kcs_draft.rectangle_new(rect) csm.Back()
def _set(self,hd): "set by a component handle" #set M,N name=kcs_draft.subpicture_name_get(self.Handle) self.M,self.N=[int(i) for i in name[2:].split('X')] #set Text and Rectangle ees=ktDraft.GetSubElements(hd) for ee in ees: if kcs_draft.element_is_text(ee): tempText=Text() kcs_draft.text_properties_get(ee,tempText) self.String=tempText.String self.Rect=kcs_draft.element_extent_get(hd)
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 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 AlignRight(): "Align texts to the left." hds=SelectTexts() tList=[] for hd in hds[1:]: t=Text() kcs_draft.text_properties_get(hd,t) tList.append((hd,t)) x=max([t[1].GetPosition().X for t in tList]) csm=ktDraft.CurrentSubpictureManager() for hd,t in tList: csm.Set(hd) rect=kcs_draft.element_extent_get(hd) width=abs(rect.Corner1.X-rect.Corner2.X) kcs_draft.element_delete(hd) t.Position.X=x-width kcs_draft.text_new(t) csm.Back()
def SetUndeeline(): "add undee(wave) line 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 pt=Point2D(x1,y) poly=Polygon2D(pt) dy=0.55 while pt.X<x2: pt.X+=1 pt.Y+=dy dy=-dy poly.AddPoint(pt) csm.Set(hdText) kcs_draft.spline_new(poly) csm.Back()
def AddNote(): res=kcs_ui.string_req("请输入标注内容") if res[0]==kcs_util.ok(): content=res[1] 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(): #开始标注 #记录原来状态 old=kcs_draft.subpicture_current_get() #创建component subView=ktDraft.GetKtSubView(SUB_VIEW_NAME) #检查是否在KNOT_VIEW下 parent=kcs_draft.element_parent_get(subView) parentName=kcs_draft.subpicture_name_get(parent) if parentName!="KNOT_VIEW": return kcs_draft.subpicture_current_set(subView) compName="NOTE_"+str(GetID(subView)) comp=kcs_draft.component_new(compName) kcs_draft.subpicture_current_set(comp) #写文字 txt=Text(content) txt.SetPosition(cor2) txt.SetColour(Colour("Red")) txt.SetFont("黑体") txt.SetHeight(5) hTxt=kcs_draft.text_new(txt) #画边框 rect=Rectangle2D() rect=kcs_draft.element_extent_get(hTxt) offset=1 rect.SetCorner1(Point2D(rect.Corner1.X-offset,rect.Corner1.Y-offset)) rect.SetCorner2(Point2D(rect.Corner2.X+offset,rect.Corner2.Y+offset)) hd=kcs_draft.rectangle_new(rect) kcs_draft.element_colour_set(hd,Colour("Red")) #画引线 sta=cor1 end=rect.GetCorner1() if end.X<sta.X: end.SetX(end.X+rect.Corner2.X-rect.Corner1.X) if end.Y<sta.Y: end.SetY(end.Y+rect.Corner2.Y-rect.Corner1.Y) line=Rline2D(sta,end) hd=kcs_draft.line_new(line) kcs_draft.element_colour_set(hd,Colour("Red")) #画圆圈 circle=Circle2D(sta,2) hd=kcs_draft.circle_new(circle) kcs_draft.element_colour_set(hd,Colour("Red")) #恢复原来状态 if len(old)==3: kcs_draft.subpicture_current_set(old[2]) else: kcs_draft.subpicture_current_set()
sList.AddString(noteName+" : "+txt.GetString()) continue while True: ee=kcs_draft.element_sibling_next_get(ee) if kcs_draft.element_is_text(ee): txt=kcs_draft.text_properties_get(ee.txt) sList.AddString(noteName+" : "+txt.GetString()) break except Exception,e: kcs_ui.message_noconfirm(e.message) sList.StrList=sList.StrList[1:] res=kcs_ui.string_select("批注","批注列表","请选择批注",sList) #显示批注列表 if res[0]==kcs_util.ok(): index=res[1] hNote=hNoteList[index-1] rect=kcs_draft.element_extent_get(hNote) #缩放 factor=50 rect.SetCorner1(Point2D(rect.Corner1.X-factor,rect.Corner1.Y-factor)) rect.SetCorner2(Point2D(rect.Corner2.X+factor,rect.Corner2.Y+factor)) kcs_draft.dwg_zoom(rect) def GetID(hSubView): """获得批注编号""" index=1 try: comp=kcs_draft.element_child_first_get(hSubView) name=kcs_draft.subpicture_name_get(comp) if name.startswith("NOTE_"): n=int(name[5:]) index=max(index,n)