프로젝트

일반

사용자정보

개정판 bf6939f4

IDbf6939f4694f25391c969a3e84e1be3cb3999810
상위 e4c359d0
하위 80a60589, 6ce0aaad

함의성이(가) 약 일년 전에 추가함

symbol insert test

Change-Id: I14ec4f3d459692ad50f4d36c7bb01a7bfa738431

차이점 보기:

DTI_PID/DTI_PID/QtImageViewer.py
745 745
                else:
746 746
                    svg.origin = [round(scenePos.x(), 1), round(scenePos.y(), 1)]
747 747
                    svg.loc = [round(scenePos.x() - svg.symbolOrigin[0], 1), round(scenePos.y() - svg.symbolOrigin[1], 1)]
748

  
748 749
                if len(svg.connectors) == 1:
749 750
                    # single connection item assistant
750 751
                    connectors = [connector for connector in connectors if connector.parentItem() is not svg and not connector.connectedItem]
......
764 765
                        #items[0].highlight(False)
765 766

  
766 767
                svg.addSvgItemToScene(scene, True if not auto else False)
768
                # symbol insert test
769
                '''
770
                elif len(svg.connectors) == 2:
771
                    matches = [item for item in scene.items() if (type(item) is QEngineeringLineItem) and (item.distanceTo([round(scenePos.x()), round(scenePos.y())]) < 20)]
772
                    svg.addSvgItemToScene(scene, True if not auto else False)
773
                    if matches:
774
                        matches[0].insert_symbol(svg, scenePos)
775
                else:
776
                    svg.addSvgItemToScene(scene, True if not auto else False)
777
                '''
767 778

  
768 779
            # svg.reSettingConnetors()
769 780

  
DTI_PID/DTI_PID/Shapes/EngineeringLineItem.py
322 322
        @author humkyung
323 323
        @date   2018.04.16
324 324
    '''
325

  
326 325
    def distanceTo(self, pt):
327 326
        from shapely.geometry import Point, LineString
328 327

  
......
332 331
        dist = line.distance(Point(pt[0], pt[1]))
333 332

  
334 333
        return dist
334
    
335
    def insert_symbol(self, symbol, pos: QPointF):
336
        """split line and join symbol and line"""
337
        import math
338
        from App import App
339
        from shapely.geometry import Point, LineString
340
        from shapely import affinity
341

  
342
        def perpendicular(pts):
343
            import math
344

  
345
            dx, dy = pts[1][0] - pts[0][0], pts[1][1] - pts[0][1]
346
            dx, dy = -dy, dx
347
            length = math.sqrt(dx * dx + dy * dy)
348
            dx /= length
349
            dy /= length
350

  
351
            return (dx, dy)
352

  
353
        def intersection(pts, line):
354
            import math
355
            from shapely.geometry import Point, LineString
356

  
357
            start_pt, end_pt = pts[0], pts[1]
358
            dx, dy = end_pt[0] - start_pt[0], end_pt[1] - start_pt[1]
359
            length = math.sqrt(dx * dx + dy * dy)
360
            if length == 0:
361
                return None
362
            dx /= length
363
            dy /= length
364
            lhs = LineString(
365
                [(start_pt[0] - dx * 20, start_pt[1] - dy * 20), (end_pt[0] + dx * 20, end_pt[1] + dy * 20)])
366
            rhs = LineString(line)
367
            return lhs.intersection(rhs)
368

  
369
        def dot_product(lhs, rhs):
370
            return sum([lhs[i] * rhs[i] for i in range(len(lhs))])
371

  
372
        def angle(pts):
373
            import math
374

  
375
            start_pt, end_pt = pts[0], pts[1]
376
            dx, dy = end_pt[0] - start_pt[0], end_pt[1] - start_pt[1]
377
            dot = dot_product((1, 0), (dx, dy))
378
            length = math.sqrt(dx * dx + dy * dy)
379
            return math.acos(dot / length)
380

  
381
        try:
382

  
383
            x1, y1, x2, y2 = self.line().x1(), self.line().y1(), self.line().x2(), self.line().y2()
384
            path = QPainterPath()
385
            path.moveTo(x1, y1)
386
            path.lineTo(x2, y2)
387

  
388
            pts = []
389
            for idx in range(path.elementCount()):
390
                ele = path.elementAt(idx)
391
                if ele.isMoveTo():
392
                    pts.clear()
393
                    pts.append((ele.x, ele.y))
394
                elif ele.isLineTo():
395
                    if len(pts) > 1:
396
                        pts.pop(0)
397

  
398
                    pts.append((ele.x, ele.y))
399

  
400
                    vec = perpendicular(pts)
401
                    line = [(pos.x() - vec[0] * 20, pos.y() - vec[1] * 20),
402
                            (pos.x() + vec[0] * 20, pos.y() + vec[1] * 20)]
403
                    origin = intersection(pts, line)
404
                    if type(origin) is Point:
405
                        dx, dy = origin.x - pos.x(), origin.y - pos.y()
406
                        length = math.sqrt(dx ** 2 + dy ** 2)
407

  
408
                        symbol.moveBy(origin.x - pos.x(), origin.y - pos.y())
409
                        symbol.loc = [round(origin.x - symbol.symbolOrigin[0], 1),
410
                                      round(origin.y - symbol.symbolOrigin[1], 1)]
411
                        symbol.size = [symbol.boundingRect().width(), symbol.boundingRect().height()]
412
                        symbol.angle = round(angle(pts), 2)
413
                        symbol.setRotation(math.radians(-symbol.angle))
414

  
415
                        if 2 == len(symbol.connectors):  # 2 way component
416
                            symbol_connectors = symbol.connectors
417

  
418
                            dx1 = symbol_connectors[0].center()[0] - pts[0][0]
419
                            dy1 = symbol_connectors[0].center()[1] - pts[0][1]
420
                            length1 = math.sqrt(dx1 * dx1 + dy1 * dy1)
421
                            dx2 = symbol_connectors[1].center()[0] - pts[0][0]
422
                            dy2 = symbol_connectors[1].center()[1] - pts[0][1]
423
                            length2 = math.sqrt(dx2 * dx2 + dy2 * dy2)
424

  
425
                            if length1 < length2:
426
                                _vertices = []
427
                                """create a new line"""
428
                                _vertices.append(self.start_point())
429
                                _vertices.append(symbol_connectors[0].center())
430
                                new_line = QEngineeringLineItem(_vertices)
431
                                self.scene().addItem(new_line)
432

  
433
                                if self.connectors[0].connectedItem:
434
                                    new_line.connectors[0].connect(self.connectors[0].connectedItem)
435

  
436
                                new_line.connectors[1].connect(symbol)
437
                                symbol_connectors[0].connect(new_line)
438
#
439
                                #"""update stream line"""
440
                                _vertices.clear()
441
                                _vertices.append(symbol_connectors[1].center())
442
                                _vertices.append(self.end_point())
443
                                self.setLine(_vertices[0][0], _vertices[0][1], _vertices[1][0], _vertices[1][1])
444
                                self.connectors[0].connect(symbol)
445
                                symbol_connectors[1].connect(self)
446
                                self.scene().update()
447
                                #"""up to here"""
448
                                return
449
                            else:
450
                                """create a new line"""
451
                                _vertices = []
452
                                _vertices.append(self.start_point())
453
                                _vertices.append(symbol_connectors[1].center())
454
                                new_line = QEngineeringLineItem(_vertices)
455
                                self.scene().addItem(new_line)
456

  
457
                                if self.connectors[0].connectedItem:
458
                                    new_line.connectors[0].connect(self.connectors[0].connectedItem)
459

  
460
                                new_line.connectors[1].connect(symbol)
461
                                symbol_connectors[1].connect(new_line)
462

  
463
                                #"""up to here"""
464

  
465
                                _vertices.clear()
466
                                connector_center = symbol_connectors[0].center()
467
                                _vertices.append([connector_center[0], connector_center[1]])
468
                                _vertices.append(self.end_point())
469
                                self.setLine(_vertices[0][0], _vertices[0][1], _vertices[1][0], _vertices[1][1])
470
                                self.connectors[0].connect(symbol)
471
                                symbol_connectors[0].connect(self)
472
                                self.scene().update()
473
                                return
474

  
475

  
476
                        """
477
                        symbol.loc = [origin.x - symbol.symbolOrigin[0], origin.y - symbol.symbolOrigin[1]]
478
                        symbol.size = [symbol.boundingRect().width(), symbol.boundingRect().height()]
479
                        self.scene().addItem(symbol)
480
                        """
481
        except Exception as ex:
482
            from App import App
483
            from AppDocData import MessageType
484

  
485
            message = f"error occurred({repr(ex)}) in {sys.exc_info()[-1].tb_frame.f_code.co_filename}:" \
486
                      f"{sys.exc_info()[-1].tb_lineno}"
487
            App.mainWnd().addMessage.emit(MessageType.Error, message)
335 488

  
336 489
    '''
337 490
        @brief  return perpendicular vector
338 491
        @author humkyung
339 492
        @date   2018.04.21
340 493
    '''
341

  
342 494
    def perpendicular(self):
343 495
        import math
344 496

  

내보내기 Unified diff

클립보드 이미지 추가 (최대 크기: 500 MB)