개정판 0f4a186f
Try to connect line to symbol
DTI_PID/DTI_PID/App.py | ||
---|---|---|
262 | 262 |
|
263 | 263 |
area = AppDocData.instance().getArea('Drawing') |
264 | 264 |
detector = LineDetector(area.img) |
265 |
|
|
266 |
symbols = [] |
|
265 | 267 |
for item in self.graphicsView.scene.items(): |
266 | 268 |
if (type(item) is SymbolSvgItem): |
269 |
symbols.append(item) |
|
267 | 270 |
#color = QColor(random.randrange(0,255), random.randrange(0,255), random.randrange(0,255)) |
268 | 271 |
res = detector.detectConnectedLine(item, int(area.x), int(area.y)) |
269 | 272 |
if res is not None: |
... | ... | |
271 | 274 |
|
272 | 275 |
if len(connectedLines) > 1: |
273 | 276 |
detector.mergeLines(connectedLines, toler=20) |
277 |
# connect line to symbol |
|
278 |
try: |
|
279 |
for line in connectedLines: |
|
280 |
matches = [symbol for symbol in symbols if symbol.isConnectable(line, toler=40)] |
|
281 |
for symbol in matches: |
|
282 |
detector.connectLineToSymbol(line, symbol) |
|
283 |
except Exception as ex: |
|
284 |
print('error occured({}) in {}:{}'.format(ex, sys.exc_info()[-1].tb_frame.f_code.co_filename, sys.exc_info()[-1].tb_lineno)) |
|
285 |
# up to here |
|
274 | 286 |
|
275 | 287 |
for pts in connectedLines: |
276 | 288 |
processLine = QEngineeringLineItem() |
DTI_PID/DTI_PID/DTI_PID.py | ||
---|---|---|
122 | 122 |
def getCoordOnRotatedImage(rAngle, x, y, originImageWidth, originImageHeight): |
123 | 123 |
rx = None |
124 | 124 |
ry = None |
125 |
if rAngle == 0:
|
|
125 |
if (rAngle == 0) or (rAngle == 360):
|
|
126 | 126 |
rx = x |
127 | 127 |
ry = y |
128 | 128 |
elif rAngle == 90: |
... | ... | |
223 | 223 |
|
224 | 224 |
def getCalculatedConnectionPoint(symbolConnectionPointStr, symbolRotatedAngle, rotateSymbolWidth, rotateSymbolHeight, originalSymbolWidth, originalSymbolHeight): |
225 | 225 |
convertedConnectionPoint = "" |
226 |
splitConnectionPointStr = symbolConnectionPointStr.split("/") |
|
227 |
for index in range(len(splitConnectionPointStr)): |
|
228 |
if index != 0: |
|
229 |
convertedConnectionPoint = convertedConnectionPoint + "/" |
|
230 |
item = splitConnectionPointStr[index] |
|
231 |
cpx = int(item.split(',')[0]) |
|
232 |
cpy = int(item.split(',')[1]) |
|
233 |
rPt = getCoordOnRotatedImage(symbolRotatedAngle, cpx, cpy, originalSymbolWidth, originalSymbolHeight) |
|
234 |
temp = str(int(rPt[0])) + ',' + str(int(rPt[1])) |
|
235 |
convertedConnectionPoint = convertedConnectionPoint + temp |
|
226 |
if symbolConnectionPointStr is not None: |
|
227 |
splitConnectionPointStr = symbolConnectionPointStr.split("/") |
|
228 |
for index in range(len(splitConnectionPointStr)): |
|
229 |
if index != 0: |
|
230 |
convertedConnectionPoint = convertedConnectionPoint + "/" |
|
231 |
item = splitConnectionPointStr[index] |
|
232 |
cpx = int(item.split(',')[0]) |
|
233 |
cpy = int(item.split(',')[1]) |
|
234 |
rPt = getCoordOnRotatedImage(symbolRotatedAngle, cpx, cpy, originalSymbolWidth, originalSymbolHeight) |
|
235 |
temp = str(int(rPt[0])) + ',' + str(int(rPt[1])) |
|
236 |
convertedConnectionPoint = convertedConnectionPoint + temp |
|
236 | 237 |
return convertedConnectionPoint |
237 | 238 |
|
238 | 239 |
''' |
DTI_PID/DTI_PID/LineDetector.py | ||
---|---|---|
1 | 1 |
import sys |
2 | 2 |
import cv2 |
3 | 3 |
import numpy as np |
4 |
import matplotlib.pyplot as plt |
|
5 | 4 |
import math |
6 |
import imutils |
|
7 |
from shapely.geometry import LineString |
|
8 |
from shapely.ops import linemerge |
|
9 | 5 |
|
10 | 6 |
class LineVector: |
11 | 7 |
def __init__(self, x1, y1, x2, y2): |
... | ... | |
143 | 139 |
except Exception as ex: |
144 | 140 |
print('error occured({}) in {}:{}'.format(ex, sys.exc_info()[-1].tb_frame.f_code.co_filename, sys.exc_info()[-1].tb_lineno)) |
145 | 141 |
|
142 |
""" |
|
143 |
@brief get distance between given two points |
|
144 |
@author humkyung |
|
145 |
@date 2018.04.13 |
|
146 |
""" |
|
147 |
def distanceTo(self, lhs, rhs): |
|
148 |
dx = rhs[0] - lhs[0] |
|
149 |
dy = rhs[1] - lhs[1] |
|
150 |
return math.sqrt(dx*dx + dy*dy) |
|
151 |
|
|
146 | 152 |
''' |
147 | 153 |
@brief rotate given point about origin by angle |
148 | 154 |
@author humkyung |
... | ... | |
350 | 356 |
print('error occured({}) in {}:{}'.format(ex, sys.exc_info()[-1].tb_frame.f_code.co_filename, sys.exc_info()[-1].tb_lineno)) |
351 | 357 |
|
352 | 358 |
''' |
359 |
@brief connect line to nearest connection point of symbol |
|
360 |
@author humkyung |
|
361 |
@date 2018.04.13 |
|
362 |
''' |
|
363 |
def connectLineToSymbol(self, line, symbol): |
|
364 |
startPt = line[0] |
|
365 |
distStart = [(self.distanceTo(startPt, pt), pt) for pt in symbol.connPts] |
|
366 |
distStart.sort() |
|
367 |
|
|
368 |
endPt = line[-1] |
|
369 |
distEnd = [(self.distanceTo(endPt, pt), pt) for pt in symbol.connPts] |
|
370 |
distEnd.sort() |
|
371 |
|
|
372 |
if distStart[0][0] < distEnd[0][0]: |
|
373 |
dx = distStart[0][1][0] - startPt[0] |
|
374 |
dy = distStart[0][1][1] - startPt[1] |
|
375 |
dir = (line[1][0] - line[0][0], line[1][1] - line[0][1]) |
|
376 |
#if abs(dir[0]) > abs(dir[1]): |
|
377 |
# line[1][1] += dy |
|
378 |
#else: |
|
379 |
# line[1][0] += dx |
|
380 |
line[0][0] = distStart[0][1][0] |
|
381 |
line[0][1] = distStart[0][1][1] |
|
382 |
else: |
|
383 |
dx = distEnd[0][1][0] - endPt[0] |
|
384 |
dy = distEnd[0][1][1] - endPt[1] |
|
385 |
dir = (line[-1][0] - line[-2][0], line[-1][1] - line[-2][1]) |
|
386 |
#if abs(dir[0]) > abs(dir[1]): |
|
387 |
# line[-2][1] += dy |
|
388 |
#else: |
|
389 |
# line[-2][0] += dx |
|
390 |
line[-1][0] = distEnd[0][1][0] |
|
391 |
line[-1][1] = distEnd[0][1][1] |
|
392 |
|
|
393 |
''' |
|
353 | 394 |
@brief detect a line along given direction |
354 | 395 |
@author humkyung |
355 | 396 |
@date 2018.04 |
DTI_PID/DTI_PID/SG_DbHelper.py | ||
---|---|---|
11 | 11 |
DB_NAME = "ITI_PID.db" |
12 | 12 |
|
13 | 13 |
CREATE_SYMBOLS_TABLE_SQL = ''' |
14 |
CREATE TABLE 'Symbol' ( |
|
14 |
CREATE TABLE IF NOT EXISTS 'Symbol' (
|
|
15 | 15 |
'uid' INTEGER PRIMARY KEY AUTOINCREMENT, |
16 | 16 |
'symId' INTEGER NOT NULL, |
17 | 17 |
'name' TEXT NOT NULL, |
DTI_PID/DTI_PID/Shapes/SymbolSvgItem.py | ||
---|---|---|
3 | 3 |
|
4 | 4 |
import sys |
5 | 5 |
import os |
6 |
import math |
|
6 | 7 |
from PyQt5.QtGui import * |
7 | 8 |
from PyQt5.QtCore import * |
8 | 9 |
from PyQt5.QtSvg import * |
... | ... | |
48 | 49 |
return QRectF(self.loc[0], self.loc[1], self.size[0], self.size[1]) |
49 | 50 |
|
50 | 51 |
''' |
52 |
@brief return true if line is able to connect symbol |
|
53 |
@author humkyung |
|
54 |
@date 2018.04.13 |
|
55 |
''' |
|
56 |
def isConnectable(self, line, toler=10): |
|
57 |
for pt in self.connPts: |
|
58 |
dx = pt[0] - line[0][0] |
|
59 |
dy = pt[1] - line[0][1] |
|
60 |
if (math.sqrt(dx*dx + dy*dy) < toler): return True |
|
61 |
dx = pt[0] - line[-1][0] |
|
62 |
dy = pt[1] - line[-1][1] |
|
63 |
if (math.sqrt(dx*dx + dy*dy) < toler): return True |
|
64 |
|
|
65 |
return False |
|
66 |
|
|
67 |
''' |
|
51 | 68 |
@brief return center of symbol |
52 | 69 |
@author humkyung |
53 | 70 |
@date 2018.04.08 |
내보내기 Unified diff